Start a Project

Launch Modes in Android

Launch Modes are the instructions on how the activities should be started. They are of four types which are given below.

  1. standard
  2. singleTop
  3. singleTask
  4. singleInstance

If you don’t define any launch mode then it will be standard by default.

How to add launch modes

You can add a launch mode from Android Manifest. The below code shows how to do it.

1. Standard

If you don’t define any launch mode then by default the activity will have standard launch mode. In this case, a new instance of the activity will be created every time you launch an activity even if it is present in the stack.

Example ->

Suppose our current stack is A -> B -> C

and we launch the Activity B again with the launch mode “standard”

then the new stack will be A -> B -> C -> B.

2. SingleTop

As the name suggests, with this launch mode only a single instance of the activity will be present if it is on the top.

Example ->

  1. Suppose our current stack is A -> B -> C
    and we launch the Activity B again with the launch mode “singleTop”
    then the new stack will be A -> B -> C -> B.
  2. Suppose our current stack is A -> B -> C
    and we launch the Activity C again with the launch mode “singleTop”
    then the new stack will be A -> B -> C and the intent will be passed to the onNewIntent() method.

3. SingleTask

With this launch mode, the activity will have only a single instance in the stack. If the instance is not there then it will create a new one and if it is there then the old one will receive the callback in onNewIntent() method.

Example ->

Suppose our current stack is A -> B -> C -> D

and we launch the Activity B again with the launch mode “singleTask”

then the new stack will be A -> B.

As you can see that the callback has been received on the old instance and C and D activities are destroyed.

4. SingleInstance

This is similar to the “singleTask” except the activity with “singleInstance” launch mode will be created in a new stack.

Example ->

Suppose our current stack is A -> B -> C

and we launch the Activity D with the launch mode “singleInstance”

then there will be two stacks

  1. A -> B -> C
  2. D

If you call activity E then it will be added to the 1st stack.

  1. A -> B -> C -> E
  2. D

Calling the activity D again will call the same activity from the 2nd stack and pass the intent to onNewIntent().

Android recommends that you should use the default behavior but if you really want to change that then this is how you can do it.

For more, you can ask me in the comment section and can also check the android developer docs.

That’s all for this blog. Thank you very much. This is Vedesh Kumar signing off.

Exit mobile version