Activity Launch Modes

Activity Launch Modes – standard

In real-world projects, we need to assign the proper launch mode for each activity based on specific requirements.

There are four launch modes:

standard, singleTop, singleTask and singleInstance

You can set the activity launchMode attribute in AndroidManifest.xml

standard is the default launch mode

If unspecified, all activities use this mode by default

Under this mode, every new activity is placed on top of the stack

The system does not check whether this activity already exists in the stack.

A brand-new instance of the activity will be created each time it is launched

Main Activity –> Normal Activity —> launch Main Activity (new instance)

For this newly created Main Activity, the system ignores the original Main Activity instance

Let’s demonstrate this below

Add a button in Main Activity. Inside the button click event, use Intent to start Main Activity again

Question? It seems we only need to press back once to exit the app

Does anyone know why this happens?

Feel free to share your findings if you figure it out.

Activity Launch Modes – singleTop

When an activity is marked singleTop, before launching it, the system checks if the activity is already at the top of the back stack. If so, the existing instance will be reused instead of creating a new one.

Let’s test this. Set the launchMode attribute of Main Activity to singleTop in AndroidManifest.xml

Now no matter how many times we tap the button, no new Main Activity instance will be created. The log only prints once.

Press back once to exit Android app

However, if this activity is not at the stack top, a new instance can still be created

Let’s try modifying the Intent this time to open another activity,

then launch Main Activity from the second activity.

Because the stack top is not Main Activity when we are on the second activity, a new Main Activity instance will be created in this case.

I/Choreographer: Skipped 37 frames!  The application may be doing too much work on its main thread.Code language: HTTP (http)

The back button exits to desktop issue from previous two lessons

This was caused by an extra finish(); statement inside our button click callback

Log.e(TAG, this.toString()); to print the memory address of the current activity instance

singleTask Mode

singleTask ensures only one instance of the activity exists across the entire application.

By contrast, singleTop only guarantees a single instance when the activity sits at stack top.

singleInstance Mode and Tasks

With this mode, the system creates a separate back stack to host this activity

For example, suppose we have three activities. If we set the second activity’s launchMode to singleInstance, it will be managed by an independent back stack.

At this point, we navigate from Activity 1 to Activity 2, then from Activity 2 to Activity 3.

When we press back from Activity 3, we first return to Activity 1, since Activity 1 and Activity 3 belong to the same back stack.

Pressing back again from Activity 1 brings us to Activity 2. The original stack holding Activity 1 is now empty, so the system switches to the top activity of another task stack, which is Activity 2. Press back once more to go back to the home screen.

public void onClick(View view){
    Intent it = new Intent(NormalActivity.this,Activity3.class);
    startActivity(it);
    finish();
}Code language: JavaScript (javascript)
05-13 08:14:26.890 4746-4746/com.foxdevelop.www.lifedemo E/MainActivity: 113
05-13 08:14:33.755 4746-4746/com.foxdevelop.www.lifedemo E/NormalActivity: 114
05-13 08:14:37.932 4746-4746/com.foxdevelop.www.lifedemo E/ThirdActivity: 113

Activity Launch Modes

Leave a Reply

Your email address will not be published. Required fields are marked *