DEV Community

Cover image for Launch modes of Android Activity
Mohit Rajput
Mohit Rajput

Posted on

Launch modes of Android Activity

Welcome to one of the most important topics of Android application development. In this article we will see detailed explanation of launch modes with examples.

Important terms

Here are a few important terms before jumping on to launch modes.

Task

A task is a collection of activities. An application can have single or multiple tasks. On application launch, a new task is created and launcher activity becomes the root of the task. New task creation depends upon launch modes which we will see later in this article.

Back Stack

Maintaining activities in the stack data structure is called the back stack. Activities can be pushed into the stack and popped from it based on user actions and launch modes.

Launch Modes

There are four types of launch modes:

  1. Standard
  2. Single top
  3. Single task
  4. Single instance

Let's understand each of them.

1. Standard

When you don't specify any launch mode, standard is the default one. It creates a new instance of the activity every time you start it. Suppose A, B, C, D, etc. are activities. It works as follows:

The task with activities:

A -> B -> C -> D
Enter fullscreen mode Exit fullscreen mode

Start new activity E:

A -> B -> C -> D -> E
Enter fullscreen mode Exit fullscreen mode

Start activity B again:

A -> B -> C -> D -> E -> B
Enter fullscreen mode Exit fullscreen mode

2. Single top

In this launch mode, if an activity is at the top of the task and you create its instance again, then a new instance will not be created. Instead onNewIntent() will be called with updated data. If activity is not on top, a new instance will be pushed. See with examples:

The task with activities:

A -> B -> C -> D
Enter fullscreen mode Exit fullscreen mode

Launch activity B. As B is not on top, a new instance will be created:

A -> B -> C -> D -> B
Enter fullscreen mode Exit fullscreen mode

Launch activity B again. As B is on top, onNewIntent() on B will be called:

A -> B -> C -> D -> B
Enter fullscreen mode Exit fullscreen mode

3. Single task

In this launch mode, if the activity doesn't exist in the task, a new instance is created otherwise onNewIntent() is called. Additionally, activities above it get destroyed. Let's understand this with examples:

The task with activities:

A -> B -> C -> D
Enter fullscreen mode Exit fullscreen mode

Start activity E with launch mode single task:

A -> B -> C -> D -> E
Enter fullscreen mode Exit fullscreen mode

Start activity B with launch mode single task:

A -> B
Enter fullscreen mode Exit fullscreen mode

Now you can see that B is already there. So a new instance is not created. The onNewIntent() of B is called. Also, C, D, and E which are above B were destroyed.

4. Single instance

For an activity that has a single instance launch mode, a new task is created. First, see the example, then we can explain it.

The task with activities:

A -> B -> C -> D
Enter fullscreen mode Exit fullscreen mode

Launch E with launch mode single instance:

Task-1: A -> B -> C -> D
Enter fullscreen mode Exit fullscreen mode
Task-2: E
Enter fullscreen mode Exit fullscreen mode

If you launch E again, it will not create a new task. In the same instance of E, the onNewIntent() method will be called.

How to add launch modes

Launch mode can be set from AndroidManifest.xml like this:

<activity android:name=”.MainActivity”
          android:launchMode=”singleTop” />

Enter fullscreen mode Exit fullscreen mode

or Java/Kotlin code using flags like this:

val intent = Intent(activity, HomeGenericActivity::class.java).apply { 
            addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        }
Enter fullscreen mode Exit fullscreen mode

See, that's very easy!!! 😁😁😁 I hope you enjoyed reading this article.

Top comments (4)

Collapse
 
arthacker8209 profile image
Deepak Kumawat

Very Informative @mohitrajput987 Thanks for sharing...

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks Deepak

Collapse
 
mehul_garg_09b3879c220685 profile image
Mehul Garg

Easy to understand and nicely explained via examples and diagrams @mohitrajput987

Collapse
 
luffy31 profile image
Raj

Great Article!