Understanding Android Launch Mode

Kurniadi
4 min readJul 31, 2021

--

Powerful way to handle your navigation stack but sometimes just not flexible enough

In this article, I will share about Android’s Launch Mode and how I as a former iOS developer try to understand with my knowledge of iOS’s Navigation behavior. Talking about launch mode means we talk about Intent’s Flag that can be used to replicate the same behavior programmatically. So i will include this thing too

Launch Mode is used to define how activity will launched on their respective task and shape task navigation. Android have 4 option, standard, singleTop, singleTask, and singleInstance. We set this value inside androidManifest.xml in android:launchMode variable.

<activity
...
android:launchMode= "selected launch mode"
... />
  1. Standard

Standard is default launch mode, we don’t even need to define it in androidManifest. In standard, everytime activity launched, it will put on the top of the task. This is the easiest launch mode to understand.

I open C Activity twice and it added to stack as new object

When we create a new intent, we don’t need to put anything on intent’s flag to replicate this behaviour programmatically

2. SingleTop

The easiest way to remember singleTop is, if an Activity is already on top of a task when same Activity launched. It won’t create new Activity. But i found this behaviour is not as simple as that so i put some condition below :

  • If target activity is already on top of the stack, It won’t add new object to stack but reuse existing activity with onNewIntent called
You will exactly just have one singleTop activity in this case.
  • If destination Activity is exist on current task but not on top. Activity will recreated
  • If destination Activity is on Top of other task. Activity will recreated in Current Task
SingleTop activity is on the top of Task 1 stack. When Task 2 launch same activity, it will create new activity and put it on top of Task 2 stack

As long as i know, iOS don’t have any capability to recreate singleTop behaviour unless manualy detecting current view controller and not navigate if current view controller same as destination view controller.

To recreate this behaviour using Intent’s Flag, just add FLAG_ACTIVITY_SINGLE_TOP when creating Intent

3. SingleTask

Simple explanation for this is, if you use this launchMode. Your Activity will be launched on new task (if it doesn’t exist), or bring to the front task who have destination Activity as root Activity, and every activity on top of that will be removed then onNewIntent called.

To creating new task, Activity must have different taskAffinity with other Task. If you using this launch mode but not setting taskAffinity, system will use default taksAffinity (application package) and will not creating new task

Equivalent for this behaviour is Intent.FLAG_NEW_TASK or Intent.FLAG_CLEAR_TOP. But watch out. onNewIntent will not be called

Like singleTop, singleTask have some condition when applied:

  • If we launch Activity in same affinity, it will finish all Activity on top of destination Activity.

in iOS, we have this kind of behaviour when use popToRootViewController or popToViewController.

  • If we launch Activity in with different taskAffinity, new Activity with launched on new task.
<activity android:name=".AffinitySingleTaskActivity"
android:launchMode="singleTask"
android:taskAffinity="com.example.affin"/>
  • If we launch Activity in with different taskAffinity, and destination activity already exist in second task, it will finish all Activity on top of destination task
Activity A, C, D finished when SingleTask triggered to launch from Task 1

4. SingleInstance

You will have exactly 1 Activity who will live in exactly 1 Task. Again, you need to add taskAffinity with different package name to make it work. If you use this launchMode without defining a taskAffinity. The original task will disappear from task manager and you can’t go back unless open the app again

No equivalent for this on Intent.FLAG

The last B Activity is triggered from singleInstance Activity, regarding whatever launchMode we put in B, it will never opened in same task which have singleInstance activity

Conclusion

Unlike iOS, we can’t see our activity stack inside a task, then we really rely on launch mode or intent flag to modify our navigation stack. Launch mode is really easy to use, but they come with price of flexibility because we can’t change the launch type in runtime.

In my experience we have more flexible way to handle navigation by using Intent’s Flag. But by using Intent’s Flag, be prepared to have some confusion because FLAG_ACTIVITY_CLEAR_TOP will not trigger onNewIntent when combined with FLAG_ACTIVITY_NEW_TASK but will call onNewIntent for FLAG_ACTIVITY_SINGLE_TOP, so be prepare to handle both onCreate and onNewIntent with same way.

When we add a Intent’s Flag and it not work like what we expect, please check this article below. Because when we start and finishing any Activity is matter

Hope this help anybody who just exploring Android. If you think my explanation is not correct or have other case that can be handled using launchMode. Don’t hestitate to leave comment and have some discussion

--

--