那个问题让我头疼了几天都没有结果。简而言之,我有两个
Activities
(活动),为了举例,我将称它们为MainActivity
和SearchActivity
。在MainActivity
中,有一个按钮可以带你到一个搜索界面,也就是SearchActivity
,那里有一个预设值的按钮,我想要把这个值传回MainActivity
。到此为止,一切都很好。我点击调试器中的按钮,我能看到我的额外值,下一个断点是在MainActivity
中的onCreate方法,但当我获取额外值时,什么也没有,全部都消失了。所以这里是重要的部分,代码如下:
MainActivity.class
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent search = new Intent(v.this, SearchActivity.class);
startActivityForResult(search, SEARCH_VIDEO_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SEARCH_VIDEO_REQUEST) {
if (resultCode == RESULT_OK) {
String url = data.getExtras().getString("VIDEO_URL");
if (url != null) {
txtUrl.setText(url);
}
}
}
}
}
SearchActivity.class
public class SearchActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String videoUrl = "http://video-url.com";
Intent data = new Intent(SearchActivity.this, MainActivity.class);
data.putExtra("VIDEO_URL", videoUrl);
setResult(RESULT_OK, data);
finish();
}
});
}
}
AndroidManifest.xml
<activity
android:name="com.project.MainActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<data android:scheme="file"/>
</intent-filter>
</activity>
<activity
android:name="com.project.SearchActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait">
</activity>
MainActivity.this
,但出现了相同的问题。 - Alex Rashkov