Cannot set both FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP in one intent?
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 a开发者_Go百科nd 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 usingFLAG_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?
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
Check this one.
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_SINGLE_TOP)
精彩评论