Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between using Intent flag "CLEAR_TOP" and launchMode="singleTask"?

What is difference between using Intent flag "FLAG_ACTIVITY_NEW_TASK" & "FLAG_ACTIVITY_CLEAR_TOP" and launchMode="singleTask"? and What is the difference in setting intent flag as FLAG_ACTIVITY_SINGLE_TOP and setting launchMode to "singleTop".

like image 349
mudit_sen Avatar asked May 11 '18 09:05

mudit_sen


2 Answers

I understood your question from your comment you replied to Tim. So you want to know the

behavior differences when setting launchModes and when setting Intentflags to an activity

Answer to your this question is that you set launchMode for an Activity inside AndroidManifest.xml file but the particular launch behavior can be changed in some ways at runtime through the Intent flags FLAG_ACTIVITY_SINGLE_TOP, FLAG_ACTIVITY_NEW_TASK, and FLAG_ACTIVITY_MULTIPLE_TASK etc.

Now lets come to your two more questions that you mentioned in your main question.

What is difference between using Intent flag "FLAG_ACTIVITY_NEW_TASK" & "FLAG_ACTIVITY_CLEAR_TOP" and launchMode="singleTask"?

FLAG_ACTIVITY_NEW_TASK

When we set this flag through intent and launch that activity. In that case that activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. It means it will create a separate history stack. For example, in your app you have a settings icon, when you click on that you go to settings activity where you have further activities. Here all the actions recorded will start from your setting activity only.

FLAG_ACTIVITY_CLEAR_TOP

As its name is telling clearly, if you start an Activity with this flag in a existing task(please understand task then everything will be very easy to understand), in that case all the activities in stack above this activity will be closed and this will become the last activity or oldest activity in the Task.

singleTask

When you start an activity for which you set launchMode = "singleTask" and there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front.

Your seconde question

What is the difference in setting intent flag as FLAG_ACTIVITY_SINGLE_TOP and setting launchMode to "singleTop"?

FLAG_ACTIVITY_SINGLE_TOP and lanuchMode = "singleTop"

Both are have same behavior, flag is set at runtime and launchMode in AndroidManifest.xml in the beginning. The behavior is, activity this with this will be the only activity on the top in a Task. If it is already running at the top of the history stack then the activity will not be launched again.

NOTE: the best way to understand the behavior is to follow any tutorial and check it practically. Play around with code and see the behavior.

Here are some useful links:

Launch Modes : https://developer.android.com/reference/android/R.styleable#AndroidManifestActivity_launchMode

Intent flags : https://developer.android.com/reference/android/content/Intent

like image 136
Vikasdeep Singh Avatar answered Sep 23 '22 21:09

Vikasdeep Singh


singleTask:- An Activity with singleTask launchMode is allowed to have only one instance in the system (a.k.a. Singleton). If there is an existed Activity instance in the system, the whole Task hold the instance would be moved to top while Intent would be delivered through onNewIntent() method. Otherwise, new Activity would be created and placed in the proper Task.You can get more information with below url:- https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

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 on top), through onNewIntent method. Note: For FLAG_ACTIVITY_CLEAR_TOP, if the launchMode is not defined in the AndroidManifest or set as “standard” for the Activity then the Activity along with its top is popped and a new instance of that Activity is placed on the top. So, onNewIntent method is not called.

This is undesirable, most of the time we would want to reuse the Activity and refresh it’s view states like, the data in the list, when it comes to the top of the back stack, rather than destroying and then recreating it.

You can get more information with below url:- https://blog.mindorks.com/android-task-and-back-stack-review-5017f2c18196

like image 33
tushar Avatar answered Sep 19 '22 21:09

tushar