Get parameters from the intent used for onResume()
Asked Answered
O

3

6

I'm using a LocalActivityManager to 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.

Oquinn answered 26/7, 2011 at 15:37 Comment(1)
It would be help if you also put the piece of your codes here.Lifelong
N
6

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.

Nymph answered 23/7, 2012 at 17:41 Comment(0)
B
2

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.

Byssus answered 13/9, 2016 at 8:9 Comment(0)
U
1

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.

Upstate answered 26/7, 2011 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.