活动以startActivityForResult()开始,未返回调用活动 [英] Activity started with startActivityForResult() not returning to calling Activity
问题描述
我有3个活动- A , B 和 C .
简而言之,活动A启动活动B,然后活动A也启动活动C,并期望获得C的结果,但永远不会得到它.
In a nutshell, Activity A starts Activity B, then A also starts Activity C and expects a result from C, but never gets it.
这是应用程序的工作流程:
Here is the application workflow:
-
活动A在应用启动时启动,并在onCreate中启动活动B(不是为了获得结果,仅是startActivity()).
Activity A is launched on app startup and starts Activity B (not for result, just startActivity()) in onCreate.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(getApplicationContext(), ActivityB.class));
}
活动A随后也会在代码中稍后启动活动C,这次使用startActivityForResult()获得结果,活动A也具有onActivityResult.
Activity A then also starts Activity C later in code, this time for result using startActivityForResult(), and Activity A also has onActivityResult.
Intent intent = new Intent(getApplicationContext(), ActivityC.class);
startActivityForResult(intent, 0);
和
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
}
活动C使用setResult()和finish()返回一些数据,这些数据应返回活动A,因为活动A名为startActivityForResult().
Activity C uses setResult() and finish() to return some data, which should go back to Activity A, because Activity A called startActivityForResult().
Intent intent = new Intent();
intent.putExtra("encryption", encryption);
setResult(56, intent);
finish();
但是,工作流在步骤3处于静默状态-永远不会调用活动A的onActivityResult(就此而言,活动B也不会被调用),即使活动A是开始于结果的C.不知道活动B是否妨碍了A和C的沟通,或者可能是什么问题.非常感谢您的帮助.
BUT the workflow falls silent at step 3 - Activity A's onActivityResult is never called (neither is B's for that matter), even though Activity A is the one starting C for result. Not sure if Activity B is getting in the way of A and C's communication or what the problem could be. Any help is much appreciated.
推荐答案
我一直无法使它正常工作,因此我最终使用了Handler来将数据返回到必要的Activity.
I was never able to get this to work, so I ended up using a Handler instead to return the data to the necessary Activity.
更新:在再次遇到此问题后,我发现这不起作用的真正原因是因为清单中的呼叫/接收活动A具有android:noHistory="true"
.删除android:noHistory="true"
可以解决此问题,但是如果需要将其设为true,则处理程序是一个不错的解决方法.
UPDATE: After running into this again, I found out that the real reason this wasn't working is because I had android:noHistory="true"
for the calling/receiving Activity A in the manifest. Removing android:noHistory="true"
fixed it, but if you need it to be true, then Handlers are a good workaround.
这篇关于活动以startActivityForResult()开始,未返回调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!