Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 317
Derek Hsu Avatar asked Apr 11 '11 13:04

Derek Hsu


People also ask

How do you add two flags in intent?

Use addFlags() so that you can add multiple number of Flags to Intent.

What is intent Flag_activity_clear_top?

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 ...

What is flag_ activity_ new_ task?

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() .

What is Flag_activity_single_top?

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().


2 Answers

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);
like image 169
Kris Van Bael Avatar answered Nov 03 '22 06:11

Kris Van Bael


This one could be helpful: Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP

like image 2
Moss Avatar answered Nov 03 '22 04:11

Moss