Launch Mode :
Instruction for Android OS which specifies how the activity should be launched.
It instrcuts how any new activcity should be associtaed with the current task.
Before we proceed further,we will discuss about Tasks & BackStack
A task is a Collection of activities that users interact withwhen performing a certain job.
When user launch an application a new task will be created and the first activity instance will be created and placed in task.That is called as “Root of the task”.
Activities are arranged with the order in which each activity is opened/
This stack is called BackStack.
When you start a new activity using startActivity(),It pushes a new activity onto task,and put previuos activity to backstack.
Once you press back button then “pops” the top most activity and remove it from the back stack .
Where to specify Launch Modes?
1.In the AndroidManifest,you can use “launchMode” attribute inside the element.
2.Use Intent Flag while starting an activity.
Types of Launch Modes:
Default Launch Mode of an activity
It creates a new instance of an activity in the task from which it was started.
Multiple instance of the same activity can be created. and that can be added to the same or different tasks.
If an instance of activity already exists at the top of the current task,a new instance will not be created,and android system will route the intent information through onNewIntent().
If an instance is not present on top of task then new instance will be created.
In this launch mode a new task will always be created and a new instance will be placed to the task as the root one.
If instance of activity exists on the separate task,a new instance will not be created and android system will routes the intent information through onNewIntent() method.
At a time only one instance of activity will exist.
It is similar to singleTask except no other activity will be created in the same Task.
Any other activity started from here will create in a new Task.
Android provides activity flags by which you can change the default behaviour of activity association with task
These flag values can be pass through intent extra data.
similar to “launchMode = singleTask”
similar to “launchMode = singleTop”
This flag will cause any existing task that would be associtaed with the activity to be cleared before the activity is started.The activity becomed the new root of an empty task.
If set,and the activity being launched is already running in the current task,then instead of launching a new instance of that activity,all of the other activities on the top of it will be closed,and this intent will be delivered to the old activty as a new intent