为什么 startActivityForResult 的结果在活动真正开始之前出现?
我有以下代码来启动活动:
public boolean onOptionsItemSelected(final MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_PREFS:
startActivityForResult(new Intent(this, PreferencesActivity.class),1);
break;
case MENU_ABOUT:
startActivityForResult(new Intent(this, AboutActivity.class),2);
break;
}
return false;
}
并使用以下代码来捕获结果:
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
switch (requestCode){
case 1:
case 2:
if (resultCode==RESULT_CANCELED)
finish();
}
但 onActivityResult 在活动(结果)开始之前触发。它的工作原理很奇怪。首先,它使用相应的 requestCode(在我的例子中为 1 或 2)和 resultCode=0 返回结果,以便我的主要活动完成。就在相应的活动(结果)真正开始之后。啥?
I have the following code to start activities:
public boolean onOptionsItemSelected(final MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_PREFS:
startActivityForResult(new Intent(this, PreferencesActivity.class),1);
break;
case MENU_ABOUT:
startActivityForResult(new Intent(this, AboutActivity.class),2);
break;
}
return false;
}
and following to catch results:
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
switch (requestCode){
case 1:
case 2:
if (resultCode==RESULT_CANCELED)
finish();
}
but onActivityResult triggers before activity (for result) starts. It works weird. First it retuns result with corresponding requestCode (1 or 2 in my case) with resultCode=0 so my main activity finishes. And right after that corresponding activity (for result) really starts. WTH?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Android 中的一个已知问题或功能。请参阅此线程,其中涵盖了类似的问题
It is a know issue or feature in Android. See this thread which covers similar problem
您的活动正在启动的活动中的代码是什么样的?被调用活动中确定何时执行 SetResult(Result.OK,1) 返回原始调用者的标准是什么?在您调用 Finish() 或执行 SetResult 指令之前,您所调用的活动不会返回。
我有运行良好的代码。
What does the code look like in the activities your activity is starting? What is the criteria in the called activity that determines when to do the SetResult(Result.OK,1) to return to the original caller? Your called activity will not return until you call Finish() or execute the SetResult instructions.
I have code that is working fine.