I'm using flag FLAG_ACTIVITY_SINGLE_TOP
and FLAG_ACTIVITY_CLEAR_TOP
to go back to my previous "standard" activity. I use FLAG_ACTIVITY_SINGLE_TOP
to prevent re-creating a new instance. But what I found is that the flag FLAG_ACTIVITY_SINGLE_TOP
is neglected and the activity is finished and re-created.
Here is what I found in docs. FLAG_ACTIVITY_CLEAR_TOP: It says that you can add FLAG_ACTIVITY_SINGLE_TOP
when using FLAG_ACTIVITY_CLEAR_TOP
to prevent "finish - recreate".
Here is another doc. FLAG_ACTIVITY_CLEAR_TOP:
Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".
Did I misunderstand the first doc?
Use addFlags() so that you can add multiple number of Flags to Intent.
flag — FLAG_ACTIVITY_CLEAR_TOP: If the Activity being started is already running in the current task then instead of launching the new instance of that Activity, all the other activities on top of it is destroyed (with call to onDestroy method) and this intent is delivered to the resumed instance of the Activity (now ...
The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .
FLAG_ACTIVITY_SINGLE_TOP. This is the same as android:launchMode="singleTop" when using it alone. If the activity exists and on the top of the task, it won't create a new instance, instead it will call the top activity's onNewIntent().
The documentation suggests that FLAG_ACTIVITY_CLEAR_TOP is all you need to set. But you actually HAVE to set both to prevent the activity from being created again.
This did the trick in my case: (Main being the activity that I wanted to return to)
Intent tabIntent = new Intent(this, Main.class);
tabIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabIntent);
This one could be helpful: Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With