Updated 28 February 2020
Launch Modes are the instructions on how the activities should be started. They are of four types which are given below.
If you don’t define any launch mode then it will be standard by default.
You can add a launch mode from Android Manifest. The below code shows how to do it.
1 2 |
<activity android:name=”.MainActivity” android:launchMode=”singleTop”></activity> |
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.
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 ->
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.
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
If you call activity E then it will be added to the 1st stack.
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.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.