Launch modes of Android Activity
Tags: android, activity, androiddevelopment, coding
Published: 2022-08-26 15:28:27


 ---
title: Launch modes of Android Activity
published: true
description: Learn about standard, single top, single task, and single instance launch modes of activity.
tags: android, activity, androiddevelopment, coding
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09es0l71any57v472g7g.png
# Use a ratio of 100:42 for best results.
---

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
```

Start new activity E:
```
A -> B -> C -> D -> E
```

Start activity B again:
```
A -> B -> C -> D -> E -> B
```

### 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
```

Launch activity B. As B is not on top, a new instance will be created:
```
A -> B -> C -> D -> B
```

Launch activity B again. As B is on top, `onNewIntent()` on B will be called:
```
A -> B -> C -> D -> B
```

### 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
```

Start activity E with launch mode single task:
```
A -> B -> C -> D -> E
```

Start activity B with launch mode single task:
```
A -> B
```

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
```

Launch E with launch mode single instance:

```
Task-1: A -> B -> C -> D
```

```
Task-2: E
```

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:
```xml
<activity android:name=”.MainActivity”
          android:launchMode=”singleTop” />

```
or Java/Kotlin code using flags like this:
```kotlin
val intent = Intent(activity, HomeGenericActivity::class.java).apply { 
            addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        }
```

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