如何在Android中管理多个活动互动? [英] How to manage multiple activity interactions in Android?
问题描述
在我的活动中,我从活动A(在后堆栈A-> B中)调用活动B,然后再从活动B(在后堆栈A-> B-> C中)调用活动C,以反映出一些结果活动A,而不是如何通过单击按钮跳转到活动A,并携带活动C的一些结果以反映回活动A(同时清除活动B,C的堆栈)
选项1 :您可以链接startActivityForResult()
的调用并在onActivityResult()
回调中展开"该链接.>
- 活动A调用
startActivityForResult()
启动活动B - 活动B调用
startActivityForResult()
启动活动C - 在活动C中先调用
setResult(RESULT_OK)
,然后再调用finish()
- 在活动B的
onActivityResult()
中收到活动C的结果.处理结果(如果需要). - 在活动B中依次调用
setResult(RESULT_OK)
和finish()
- 在活动A的
onActivityResult()
中收到活动B的结果.处理结果.
选项2 :再次启动活动A,但向意图添加标志.
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// add more data to Intent
startActivity(intent);
finish();
活动A的onNewIntent()
方法中将收到此意图.
编辑
选项3::使用LocalBroadcastManager
在本地发送广播(仅在您的过程中).这要求在活动A中动态注册BroadcastReceiver
,然后从活动C发送广播.您仍然需要使用上述两种技术之一返回活动A并清除堆栈;否则,您将无法使用它.这只是改变了将数据传输回活动A的方式.
In my activity, i'm calling activity B from activity A (in back stack A->B), than again activity C from B (in back stack A-> B-> C) for some result to reflect on activity A, than how to jump to activity A on button click with carrying some result from activity C to reflect back in activity A(while clearing back stack of activity B,C)
Option 1: You can chain calls of startActivityForResult()
and "unwind" the chain in the onActivityResult()
callbacks.
- Activity A calls
startActivityForResult()
to start Activity B - Activity B calls
startActivityForResult()
to start Activity C - Call
setResult(RESULT_OK)
and thenfinish()
in Activity C - Result of Activity C is received in
onActivityResult()
of Activity B. Process the result (if needed). - Call
setResult(RESULT_OK)
and thenfinish()
in Activity B - Result of Activity B is received in
onActivityResult()
of Activity A. Process the result.
Option 2: Start Activity A again, but add flags to the intent.
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// add more data to Intent
startActivity(intent);
finish();
This intent will be received in the onNewIntent()
method of Activity A.
EDIT
Option 3: Use LocalBroadcastManager
to send broadcasts locally (within your process only). This requires registering a BroadcastReceiver
dynamically in Activity A, then sending the broadcast from Activity C. You will still need to use one of the two techniques above to get back to Activity A and clear the stack; this just changes how the data is transmitted back to Activity A.
这篇关于如何在Android中管理多个活动互动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!