开发者

Get parameters from the intent used for onResume()

I'm using a LocalActivityManager t开发者_StackOverflowo have activities in different tabs, when I switch from a tab to another one, I start the Activity corresponding to the tab selected. My problem is simple :

if I click on tab 1, I create intent11 and the first time, the method onCreate(Bundle emptyBundle) of Activity1 is called. If I click on tab 2, I create intent2 and the method onCreate() is called. Then, when I click on tab1, I create intent12 , the method onCreate(Bundle emptyBundle) is not called but onResume() is called (normal behavior).

I put special extras in the intent11 and intent12 to create Activity1, so I access it using getIntent().getExtras().

My problem is : the second time I go to the tab1, the intent12 is used to start the Activity, but the result of getIntent() is still intent11. So I can't retreive the extras set in intent12, I can only retreive the extras set in intent11.

What am I doing wrong ? Should I avoid putting extras() in the intents ? Thank you.

Thank you.

PS : for the moment, I set a special flag to my intent to force to call onCreate(), but I'm sure it's not the good way of doing it.


I believe what you are looking for is here: https://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29

onNewIntent(Intent newIntent) allows you to override the previous intent that was used to create/resume the app with the newest intent.


In Xamarin.Android / Monotouch I just added the following method to my Activity and it worked smoothly.

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        Intent = intent;
    }

The principle should work fine also in Native Android.


no you should be able to still put the extras but I'm wondering if the extras are getting 'overwritten' when you are creating the new intents so I suggest trying this:

Put your extras into the bundle for the first intent you create, then before creating the next intent set your bundle to whatever might be in the bundle already by doing

Bundle bundle = getResultExtras(false);

Then you can create your new intent then when you are ready to get your data out of the bundle you can do

Bundle bundle = getResultExtras(false);

again and then get your data like you normally would from the bundle, just make sure that the extras your put in Intent1 don't have the same key name as the extras you put in Intent2

hope that helps some.

if you need more specific help it might be useful to post your code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜