Android “launchMode” (Visualized)

Mert SIMSEK
3 min readMar 29, 2017

--

Android launchMode is important thing and all android developers should know how launchMode works. Most of developers (including me) think that we know why it is used. But we are wrong. It is not just about making activity singleTask to prevent duplicate instance. OK you are right. It solves our problem but do we really know what happens to other activities in the stack when we make activity singleTask?

There is a really really good blogpost about how launchMode works. Thanks to Amit for blogpost. But I want to visualise it to be understandable at first glance. I will show just launchMode that we use in AndroidManifest. Next blogpost I will visualise intent flags.

First of all I want to give you two image to make it more understandable.

Green one means new fresh activity. Yellow one means same activity instance but onNewInten() method is called.

1 — launchMode = “singleTop” in Activity D

1. example If you define Activity D as singleTop in AndroidManifest.xml. D uses same instance and onNewIntent() method is called in Activity D.
2.example If you define Activity D as singleTop in AndroidManifest.xml. Fresh new activity is created.
3. example If you define Activity D as singleTop in AndroidManifest.xml. D uses same instance and onNewIntent() method if and only if it is already on top of task. If it is not on top of task, then new Activity D instance will be created.

2 — launchMode = “singleTask” in Activity C

You see that Activity D is destroyed? and onNewIntent() method is used in Activity C.
Fresh new Activity C is launched.

3 — launchMode = “singleInstance” in Activity E

Woaw!? yes. A,B,C,D is in one task. E will be created in another task.

Yes. Activity E is created in new task. What if we want to launch Activity F from Activity D? Here is the result;

E is still in another task. a,b,c,d and f are in another task.
Activity E is still in another task. And If we start it again, onNewIntent() method is called instead of creating new fresh activity.

4 — launchMode = “standard” in Activity B

Creates fresh new activity B. And yes I know we already have one B in stack. But It does not use that one.

Happy coding.

Recommend it If you like it :) Thanks to Amit Shekhar.

--

--