Activity|LaunchMode|With Lifecycle

Before starting development in any platform first, we should understand the ecosystem of the platform. If you are creating an application in android than the first thing you will interact with Activity.

So let me guess some obvious questions in your mind as a beginner.

  1. What is the activity in android?
  2. The lifecycle of activity(Behaviour of activity with the android ecosystem)?
  3. How many ways to use activity (Launch modes)
  4. What are the changes in the activity lifecycle with launch modes?

So here we start:

  1. In Android, An Activity is one screen of the Android app’s user interface. In that way, an Android activity is very similar to windows in a desktop application, In general, whatever you see in the application comes under the activity.
  2. Any Android activity goes through a certain life cycle during its life inside the Android app. There are so many callbacks method states which get notified from the android ecosystem when the state of activity gets change from creating to destroy.
    Some important and most used of them :
    A) onCreate() → Gets a call when activity is launched or created.
    B) onStart() → Gets a call when activity is started and visible.
    c) onResume() → Gets a call when activity is ready for interaction with the user.
    D) onPause() → Gets a call when activity is not interacting with the user or partially visible.
    E) onStop() → Gets a call when activity is in the background or not visible to the user.
    F) onRestart() → Gets a call when activity is coming back to the foreground from the background.
    G) onDestroy() → Gets a call when activity is no longer available.

3. There are 4 ways to launch the activity which is called launch modes.

By default, activity maintains the stack of activity in task manager all the launch mode is just to maintain smartly the task manager and behavior of that accordingly.

Note: We can set launch mode in manifest in activity tag as well as in runtime also we can choose as per our need.

<activity
android:name=".MyActivity"
android:launchMode="singleTask">

1) Standard, →

This is the default launch mode of activity. If you don’t set any launch mode to your activity, it will use the standard mode by default. It creates a new instance of activity every time even if the activity instance is already present.

For example, we have A, B, C, and D activities and activity B has a standard launch mode.

I launched the app and gone from A to D here is flow

In Activity Stack

A →B→ C→D

Now launch Activity B from D again

A → B → C→D→ B

You can see a new instance of Activity B is created again hence every time it will create a new instance of the activity.

2) SingleTop

If an instance of the activity already exists at the top of the current task, a new instance will not be created which means if you have B activity on top and again you launch B activity than only onNewIntent() callback method will be called.

Example

we have A, B, C, and D activities and activity B has a singleTop launch mode.

In Activity Stack

A →B→ C→D

Launching Activity B from D

If an instance of B is not present on top of the task then a new instance will be created.

A →B→ C→D →B

Now B is on the top again I am Launching B from B which is on top.

In Activity Stack

A →B→ C→D →B

No new Instance will be created only newIntent() method will be called.

Before going forward let me bring something in the notice called TaskAffinity

TaskAffinity is a tag in the manifest file which we can give with the launch mode and it can have value any string by default it will be having a value of application package. We can use it with SingleTask and SingleInstance launch mode it will affect the activity task manager behavior so use it wisely.

By default, the application will have package name affinity so all the activity will be part of the same task if we want to change the task of the activity so we can use TaskAffinity.

If you don’t give any task affinity so it will have default one.

3) SingleTask → If an activity is declared with SingleTask will have only one instance in the task manager doesn't matter how many times you launched activity. In general, it is singleton behavior.

For example(Without Affinity)

we have A, B, C, and D activities and activity B has a singleTask launch mode.

In Activity Stack

A →B→ C→D

If an instance of B is already there in the stack than all the activity tasks will be removed whatever on top of B and B will be brought in the top.

Now I am launching B from D

So D and C are removed from the task.

Now the stack is having

A →B ( B is on the top of stack )

No new instance will get call only intent data route through onNewIntent()

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Example (With affinity)

we have A, B, C, and D activities and activity B has a singleTask launch mode with affinity in the manifest.

Launching A and

state of →Task Manager →A

Now Launch B which is having task affinity

state of →Task Manager (let’s say 1)→A

state of →Second Task Manager(let’s say 2) →B

Now Launch C from B

state of →Second Task Manager →B →C

and now Launch D from C

state of →Second Task Manager →B →C →D

Launching Activity B from D

both Activity D and C will be removed

state of → Second Task Manager →B

Now if you back press than task manager and activity B will be destroyed. and A activity and Task manager will be brought in front.

state of →Task Manager →A

In general, if you define taskAffinity so later on whatever activity you will launch will be part of a new task which is created by B.

4) SingleInstance

It is similar to SingleTask if you don't give task affinity. But If you give task affinity so the behavior gets change. let's see how

Example (Without task affinity)

we have A, B, C, and D activities and activity B has a SingleInstance launch mode with affinity in the manifest.

Launching A

state of →Task Manager →A

Now Launch B which is having task affinity

state of →Task Manager (let’s say 1)→A

state of →Second Task Manager(let’s say 2) →B

Now Launch C from B

state of →Task Manager → A →C

state of →Second Task Manager →B

Note that the activity which is getting launch is the part of Task Manager 1

Now launch D from C

state of →Task Manager → A →C →D

state of →Second Task Manager →B

Example (With task affinity)

The behavior remains the same as without task affinity the only difference you can see is tapping on the recent app button you will get a separate instance of Activity B.

Conclusion:

  1. Use notification activity as singleTop or SingleTnstance based on the requirement.
  2. Use normal flow as standard.
  3. Use singleTask or singleInstance with 3rd party application launch like any payment gateway.
  4. FLAG_NEW_TASK will create activity own task that is a new task for new launch activity.
  5. FLAG_CLEAR_TASK|FLAG_NEW_TASK both used together to clear the task and start a new activity with the blank task. Initially, A new activity will only be part of the new task.
  6. CLEAR_TOP All other activity gets destroyed and the activity with clear top will get onNewIntent() callback.

Senior Android App Developer | Startup | Product base | Java | Kotlin | MVVM | Architecture components | Android Blogger