开发者

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. In A I want to check if there is a bundle passed (the first time, start-up, there won't be one for example). The data is passed from A-B with a normal bundle. From B-->C I use this:

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:

  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:

 @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. When I passed it back from C-->B there was no problem. 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. 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开发者_StackOverflow(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. C has no way of knowing it should return its result to A, as it is only aware of Activity 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:

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:

Intent data = new Intent("my.action");
data.putExtra("point", point);
data.putExtra("noqs", noqs);
sendBroadcast(data);


When you finish activity B, why are you using a new Intent instead of the getIntent() method?


You may wish to use Fragments instead of activities in your game and manage the state transition within the FragmentManager instead of trying to use the Activity stack. It seems like it might be a bit easier to manage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜