Launch modes

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:

4.Single Instance

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.

Intent Flags:

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



What is Activity?

Activity is an individual User interface screen where visual elements are called views or widgets.

Two different ways to create widgets are

1.Java Code,
2.XML Code

Application can have more than one activity

Each Should be defined in manifest.xml

Each Activity in android is a subclass of Activity class in Android SDK.

Four States of Activity?

1.Active or running


Activity Life Cycle?


Callback methods : 

onCreate() : This is the first callback and It is called when the activity is first created.

onStart() : This call back is called when the activity is visible to the user.Still not active

onResume() : Activity is visible and active for user interaction.Activity will be placed on top of activity stack.

onPause() : Called when user starts another activity or navigates to some other feature of system.This callback is last guarantee call before destroying activity.

onStop(): Called when activity is no longer visible.

onDestroy(): Called before destroying activity

onRestart() : Called when the activity restarts after stopping it.

onPause State

1.User pressed HOME button
2.Another activity on top does not completely obscures the visibility.
3.The device goes to sleep
4.It gets killed by system under low memory conditions.No methods will be called before destruction of activity.Need to rerun by calling onCreate(),and restore previous configuration by using bundle object.
5.User resumes the activity by calling onResume().

Stop State

1.System kills to free the resources.It needs to restart cycle again by calling onCreate().
2.It is restarted by calling onRestart(),onStart(),onResume().In this case no need to restore

Activity Lifecycle Loops

Three Life cycle loops exists for every activity.They are

1.Entire Lifetime: Between onCreate() – onDestroy()
2.Visible Lifetime: Between onStart() – onStop(),User May or May-not be able to interact with activity.
3.Foreground Lifetime : Between onResume() – onPause(),User can interact with activity,On top of all other activity. Completely visible

Reference Code :