avatarElye - A One Eye Dev By His Grace

Summary

The website content provides an explanation of Android Activity launch modes, detailing the behavior of standard, singleTop, singleTask, and singleInstance launch modes and their impact on activity instances and task management.

Abstract

The article on the website delves into the concept of Android Activity launch modes, which are crucial for managing the lifecycle and instances of activities within an Android application. It explains that the launchMode attribute in the AndroidManifest.xml file can be set to one of four values: standard, singleTop, singleTask, or singleInstance. Each mode dictates how activities are launched and managed in the activity stack. The standard mode creates a new instance of the activity every time it's launched, while singleTop prevents a new instance if the activity is already at the top of the stack, instead calling onNewIntent. The singleTask mode ensures a single instance of the activity exists across the entire application, and singleInstance isolates the activity in its own task, independent of other activities. The article also discusses the implications of mixing these launch modes and provides visual examples to illustrate the behavior of each mode. The author encourages developers to experiment with different launch modes to gain a better understanding and to design more intuitive activity navigation.

Opinions

  • The author suggests that understanding Android Activity launch modes is essential for developers working with multiple activities in an app.
  • It is implied that the choice of launch mode can significantly affect the user experience, with standard and singleTop being less confusing as they preserve activity order.
  • The author indicates a personal need for clarification on certain launch mode combinations, which led to the creation of a demonstration app to explore these scenarios.
  • A recommendation is made for developers to use the provided demonstration app to experiment with launch modes and observe their effects firsthand.
  • The author emphasizes that a solid understanding of launch modes can lead to better-designed activity navigation within Android applications.

Learning Android Development

Android Activity LaunchMode Made Simple

Understand when to use which activity launch mode.

Photo by SpaceX on Unsplash

When we declare our Activity in AndroidManifest.xml, there’s a little attribute we can set, which is launchMode. It has 4 possible settings i.e. standard, singleTop, singleTask and singleInstance.

E.g.

<activity android:name=".MyActivity"
    android:launchMode="standard" />

If you are using a single Activity App, these don’t matter. But if you have multiple activities in your app, you might want to know what.

A great illustration is provided at https://iammert.medium.com/android-launchmode-visualized-8843fc833dbe. But I still have questions about some other combinations of launch, hence I make an App that I can just demo any combination.

With a clear understanding of how each works, let me describe and make it simple for all.

Standard launch mode

This is the default when no launchMode is set. Its behavior is simply just launched a new activity every time it is launch, even for the same activity already previously launched or the activity on the top.

Duplicated instances of the activity always happen when a launched activity gets relaunch again.

Notice all activities is created a new even if they already were launched previously

SingleTop launch mode

This is similar to standard with the exception, if the activity already at the top, it will not get relaunched. Instead onNewIntent will happen instead on the existing activity.

But if the activity is not at the top, a duplicate new activity can be launched.

Notice activity C is not relaunched as it was already at the top

SingleTask launch mode

This launch mode

  1. always ensures that there is only a single copy of the activity exist
  2. retract back to where the activity is if it is relaunched i.e. pop away all other activities on top of it
Notice the top activity C and B get pop away as we try to launch new activity B and A

SingleInstance launch mode

This is a special launch mode, making each activity independent from others, as they are not in the same task.

Each activity

  1. can only be launched once
  2. relaunch of it will bring it to the top again without popping others
Notice that I separate each box out, as they are in different tasks.

The mixture of SingleInstance and others

If we mixed up standard, singleTop and singleTask, the behavior is still the same as they are by themselves. They are treated as a group of activities together within a task.

When they are mingled with singleInstance, the entire group will be treated as a single instance itself, e.g. they get reordered accordingly.

Notice that Task A, B, C moved down altogether with Instance A get relaunched.

Summary

On the above, I just provide a few samples of the launch order combination. If you have wonder how other combination is behaving, feel free to get the I code, compile and experiment it.

To sum up, I made a simple table below.

As you can see, only standard and singleTop really preserve the order, hence it will less likely confuse users when using it. Therefore it is the recommended launchMode by Google.

Nonetheless, having a solid understanding of the launch mode will make one design better navigation of activities.

Android App Development
Mobile App Development
AndroidDev
App Development
Android
Recommended from ReadMedium