[英]Pass bundle between three activities in Android
I have three activities that I can call A, B and C. I want to pass information between the three like this: A-->B-->C-->A. 我有三个活动,我可以称之为A,B和C.我想在这三个活动之间传递信息:A - > B - > C - > A. In AI want to check if there is a bundle passed (the first time, start-up, there won't be one for example). 在AI中想要检查是否有传递包(第一次启动时,例如不会有一个)。 The data is passed from AB with a normal bundle. 数据从AB传递,带有正常捆绑。 From B-->CI use this: 从B - > CI使用此:
Intent i = new Intent(getApplicationContext(), FlashcardView.class);
i.putExtra("rattning", rattning);
i.putExtra("noqs", noqs);
i.putExtra("categoryid", categoryid);
CreateTestView.this.finish();
startActivityForResult(i, 0);
It is received and then sent onwards to A like this: 它被收到然后发送到A这样:
Intent data = new Intent(FlashcardView.this, MenuView.class);
data.putExtra("point", point);
data.putExtra("noqs", noqs);
setResult(RESULT_OK, data);
finish();
It is received at A like this: 在A收到这样的:
@Override
protected void onActivityResult( int req, int resp, Intent data ) {
super.onActivityResult(req, resp, data);
// process your received "data" from GameActivity ...
Bundle b = getIntent().getExtras();
noqs = b.getInt("noqs");
point = b.getInt("point");
mTvCat.setText("hhhhhh"+point+noqs);
publishOnFacebook(point,noqs);
}
It seems though like the bundle is lost on the way from C-->A. 看起来好像捆绑在从C - > A的路上丢失了。 When I passed it back from C-->B there was no problem. 当我从C - > B传回来时没有问题。 I think this happens cause B is the activity that starts C, and therefore C falls back to B, not A. I made a go-around by calling finish() on B, so C goes back to A instead. 我认为这是因为B是启动C的活动,因此C回落到B而不是A.我通过在B上调用finish()进行复飞,所以C回到A而不是。 BUt in doing this I lose the bundle. 坚持这样做我失去了捆绑。 Or at least I think so. 或者至少我是这么认为的。
Has anyone seen this before? 谁看过这个吗? Is there a better way of doing this? 有没有更好的方法呢? How can I prevent losing the bundle on a passing between more than two activities? 如何在两次以上活动之间传递时防止丢失捆绑? THanks! 谢谢!
Edit: 编辑:
Edit: Here is the error code on receiving the broadcast: 编辑:这是接收广播时的错误代码:
03-07 12:57:59.394: ERROR/AndroidRuntime(803): java.lang.RuntimeException: Error receiving broadcast Intent { act=my_action (has extras) } in com.crystalcodeab.Flashcard.MenuView$1@47681ad0
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:981)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.os.Handler.handleCallback(Handler.java:587)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.os.Looper.loop(Looper.java:143)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at android.app.ActivityThread.main(ActivityThread.java:5068)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at java.lang.reflect.Method.invoke(Method.java:521)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at dalvik.system.NativeStart.main(Native Method)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): Caused by: java.lang.NullPointerException
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at com.crystalcodeab.Flashcard.MenuView$1.onReceive(MenuView.java:56)
03-07 12:57:59.394: ERROR/AndroidRuntime(803): at
I think the problem lies in how you finish activity B. When you finish Activity B, the result from B is sent back to A and the Activity is gone. 我认为问题在于你如何完成活动B.当你完成活动B时,B的结果被发送回A并且活动消失了。 C has no way of knowing it should return its result to A, as it is only aware of Activity B. C无法知道它应该将结果返回给A,因为它只知道活动B.
My suggestion to you is that you should use a BroadcastReceiver in Activity A. Then in Activity C you can send a Broadcast with the data you wish to be received by A: 我的建议是你应该在活动A中使用BroadcastReceiver。然后在活动C中你可以发送一个广播,其中包含你希望被A接收的数据:
In A: 在一个:
IntentFilter filter = new IntentFilter("my.action");
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("my.action")) {
// Do my stuff
}
}
}
registerReceiver(receiver, filter);
In C: 在C:
Intent data = new Intent("my.action");
data.putExtra("point", point);
data.putExtra("noqs", noqs);
sendBroadcast(data);
You may wish to use Fragment s instead of activities in your game and manage the state transition within the FragmentManager instead of trying to use the Activity stack. 您可能希望在游戏中使用Fragment而不是活动,并在FragmentManager中管理状态转换,而不是尝试使用Activity堆栈。 It seems like it might be a bit easier to manage. 似乎管理起来可能更容易一些。
完成活动B后,为什么使用新的Intent
而不是getIntent()
方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.