【发布时间】:2012-04-06 20:21:25
【问题描述】:
我有 2 个活动,每个活动都在单独的应用程序中。 Activity1 有一个用户可以单击的按钮,它使用其onClick() 方法中的意图调用第二个活动:
Intent myIntent = getPackageManager().getLaunchIntentForPackage(com.myProject.Activity2);
startActivityForResult(myIntent, 600);
这会正确地从 Activity1 启动 Activity2,但 onActivityResult 在 Activity2 中调用 onCreate 之前在 Activity1 中被调用,而不是在我设置返回意图的 onBackPressed() 中。
这里是 Activity2 的onCreate 方法:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
这是 Activity2 的 onBackPressed 方法的当前版本:
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("Stuff", someStuff);
if(getParent()==null){
setResult(Activity.RESULT_OK, intent);
}else{
getParent().setResult(Activity.RESULT_OK, intent);
}
finish();
super.onBackPressed();
}
我的 AndroidManifest.xml 有以下 Activity2 的意图过滤器:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
我验证了我的launchMode 是standard(而不是singleTask 等),正如here 所建议的那样,并且我的请求代码并不像here 所警告的那样为负。我也试过android:launchMode="singleTop",但那也是不行的。
我还尝试在onBackPressed() 中不为 Activity2 调用finish(),如提到的here (也只使用super.onBackPressed(),如建议的here)并再次按照建议的here 调用它。
此外,我尝试将 intent.putExtra("Stuff", someStuff); 行注释掉,因为它似乎会给 this person 带来麻烦。
关于我可能做错了什么有什么想法吗?
【问题讨论】:
标签: android android-intent android-activity