mudit_sen
mudit_sen

Reputation: 1510

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

Upvotes: 13

Views: 4461

Answers (2)

Vikasdeep Singh
Vikasdeep Singh

Reputation: 21766

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

Upvotes: 11

Norbert
Norbert

Reputation: 1244

I think the Android documentation https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP of FLAG_ACTIVITY_CLEAR_TOP explains the difference quite well. The launch modes are quite nicely explained at https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en.

Upvotes: 0

Related Questions