4

In my navigation the following sequence of activities can be produced: A,B,C,B,C,B,C, ..., D.
D activity contains an overriden method for the Back-button, which sends user to the A activity with Intent.FLAG_ACTIVITY_CLEAR_TOP parameter.

Activity A receives a number of necessary extra parameters and my problem is that when I trigger startActivity in D activity with this CLEAR_TOP flag activity A doesn't seem to get any previous extra or even launch onResume method.

Does it mean that CLEAR_TOP actually recreates the target activity instead of bringing up the old one ? And, is there any system-natural approach to restore those extra intent parameters in activity A when I launch it from activity D.

P.S. Right now the only choice I see is to manually fillin those previous necessary parameters when constructing intent object in D activity. Is it a way to go ?

Thanks.

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167

3 Answers3

4

It's as simple as the docs says.

If you set both FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP, then new parameters will be pushed into onNewIntent().

4

Not sure if you've found a solution to this or not, but overriding the onNewIntent(Intent theNewIntent) method for the target activity & calling setIntent(theNewIntent) solved it for me.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    /*
     * This overrides the original intent.
     */      
    setIntent(intent);
}
Ryhan
  • 1,815
  • 1
  • 18
  • 22
  • Before the call to super.onNewIntent, the old intent is still accessible and you can capture any needed extras and add them to the new intent. – ProjectJourneyman Aug 24 '18 at 05:54
2

Does it mean that CLEAR_TOP actually recreates the target activity instead of bringing up the old one ?

When you use CLEAR_TOP flag,target Activity does not recreate,but when you use a new intent to appear Activity A,you can only get this new intent's extras in Activity A.

And, is there any system-natural approach to restore those extra intent parameters in activity A when I launch it from activity D?

You can save these extras in file,preferences or global application variables,when you leave Activity A.Or you can declare a static method in Activity A that save these extras in it and you would to invoke it from Activity D,before force to appear Activity A:
Activity A:

Public class A extends Activity{
...
public static void saveExtras(){
//save extras in file,preferences,...
}
...
}     

Activity D:

Public class D extends Activity{
...
public void forceActivityA(){
A.saveExtras();
Intent i = new Intent(this,A.class());
...
startActivity(i);
}
...
} 

Edit:
When Activity A appears again,it's onCreate() method invokes again and here you can get intent that request it.Then you can get intent's flag and compare it with 67108864(constant for CLEAR_TOP flag).If it equals to this constant you would to retrieve your data from your storage(file,preferences,...).To create global variables you can see this question.

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • with this code `Intent i = new Intent(FourthActivity.this, SecondActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i);` SecondActivity's onCreate gets triggered, so I guess it does recreate the whole activity. Yet I don't see any solution to my problem - what would you recommend to restore those extra parameters ? Should I look into onSaveInstanceState method ? –  Jul 15 '12 at 09:17
  • @Дмитрий Филюстин please see my edits again.Thanks you for your check mark as answer. – hasanghaforian Jul 15 '12 at 09:54