Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP

Tags:

I have an app that I have running a media player and I want to resume the activity from my apps home activity.

I can successfully do this by adding the following flags to the startActivity Call:

myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

I am worried that this is not an ideal way to do things since it took me a long time to find it. It made me think that no one uses it very much.

Are there any pitfalls to using this method?

like image 932
shaneburgess Avatar asked Jul 10 '10 05:07

shaneburgess


People also ask

What is Flag_activity_clear_top?

FLAG_ACTIVITY_CLEAR_TOP. If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent() ).

What is intent Flag_activity_new_task?

FLAG_ACTIVITY_NEW_TASK is equivalent to launchMode=singleTask and in there I read. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.

What are intent flags in Android?

Use Intent Flags Intents are used to launch activities on Android. You can set flags that control the task that will contain the activity. Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front.


2 Answers

I know that this question is quite older and may you have solved your problem and may be travelled to mars and back in those years.But just to make things clear for the people coming here and looking for an explanation:

According to Official Documentation:

If set, the activity will not be launched if it is already running at the top of the history stack.

  • Suppose you have BackStack of A-B-C-D and You have launched another intent Starting Activity A with FLAG_ACTIVITY_CLEAR_TOP at this point what android will be do is Simply Clear All the Activities on the Top of Activity A, means now your BackStack Will Look like-> A (yes that's it because you have cleared all the activities on top of it).
  • And In Second Scenario Suppose You have the same BackStack A-B-C-D and you launched an Intent Starting Activity A with FLAG_ACTIVITY_SINGLE_TOP, now your BackStack will look like-> A-B-C-D-A (Confused? Don't Worry Next Example Will Clear All Your Confusions)
  • Third Scenario, We start with the same BackStack A-B-C-D and We will launch an Intent Starting Activity D with FLAG_ACTIVITY_SINGLE_TOP, now our BackStack will look like-> A-B-C-D (Yes the BackStack remains in the Same Order because our FLAG prohibits the launch Same Activity If it is the Currently on Top of the Stack and Showing on the User screen.

  • Last Scenario, We start with our same BackStack A-B-C-D and We will launch an Intent Starting Activity D but this time with no FLAGS, now our BackStack will look like-> A-B-C-D-D.

Got it? FLAG_ACTIVITY_SINGLE_TOP is only useful When you are starting the Same Activity Which is Currently Showing On the Screen and You want to prohibit the launch new Instance of the Existing Activity.

like image 187
Shubhamhackz Avatar answered Oct 29 '22 20:10

Shubhamhackz


Just to make sure I understand your question correctly: Say your activity stack is something like A -> B -> C, and now from C you want to clear the stack and get back to A.

If this is what you want, then those intent flags are correct and should work as expected, as per the Android-developer docs.

like image 45
Thinkisto Avatar answered Oct 29 '22 18:10

Thinkisto