Tasks Explained in Android

Harjot Singh
4 min readOct 26, 2022

--

Consider this scenario, you click on the launcher icon of your application and boom! The application starts ✌️
Simple right? Okay…you might say it takes up some memory and then user interacts with it and that’s it.

But is that really it??
From the outside, well yes but let’s hop into how this is actually handled under the hood.

Before starting let’s do some justice to the title 😅

What actually is a task?
A task is a stack of activities. Straight-forward right.
Now what is actually happening under the hood?

As soon as you click the launcher icon, the system searches for a previously existing task so that it could resume that one and if not found, then a new task is created with your newly created activity . That newly created activity is added to back stack of your task. Say, user started an application and Activity 1 opens up, now from Activity 1 the user goes to Activity 2…here’s how the back stack for the task will look like:

As per the definition, Task is based on STACK data structure. As soon as startActivity is called the new activity is pushed into the back stack. Now, say from activity 2 user hits back button then we pop-out the last activity.
LIFO (Last-in First-out) is followed here just like STACK.

This is true for Fragments too.

For the above example, Activity 2 hosts a fragment. Now, this fragment, say… does a few replacement transactions and add those transactions to the back stack. Now when user hits back button, firstly the transactions gets undo’d and then Activity 2 is popped off.

Easy enough?
Now, this is when things get a bit spicy!

  • A task can have multiple instances of the same activity and an application can have multiple tasks.
    But how?

A task having multiple instances of same activity is possible. If the requirement is only single instance of that activity should be in the back stack, we can make use of singleTask or singleInstance as the launchMode attribute.
(More on this in my upcoming blog on Android LaunchModes. Stay tuned!)

An application can have multiple tasks…what’s the need though?

Consider Gmail application for that matter. When Gmail is launched, the inbox is the first screen shown. Say, user wants to compose a new email…separate window opens up. Now, if you click on the overview screen (the one next to home button)…you’d see two windows of Gmail, one for composing email and one for the inbox.
The use case is simple, whenever user wants to switch between different pieces of your application multiple tasks are required. As per our current example, user can reference back to the inbox whilst drafting a new email.

What if no one thought about multiple tasks, writing an email using reference from a previous email would have been such a tedious job then 😰

Side note: Multiple tasks are also known as Concurrent Documents where Concurrent means tasks in parallel and Document simply means separate.

The real question….How to create multiple tasks for an application?
Well, that can be done using android:documentLaunchMode attribute in the manifest or using Intent flags.

Within manifest:

<activity
android:name=".food_logs.view.SomeNewActivity"
android:label="@string/some_new_title"
android:launchMode="singleTask"
android:documentLaunchMode="always"
/>

intoExisiting: The activity reuses the existing task for the document

always: The activity creates new document for the task every time.

none: The activity creates the task in the same stack and this is the default behaviour of any registered activity.

never: This overrides any specified flags and keep the task in the current stack.

Using Intent flags:

Intent.FLAG_ACTIVITY_NEW_DOCUMENT: It tells the system to treat next task as a separate window in the overview screen or reuse an existing one for the same component. This is equivalent of intoExisting.

val intent = Intent(this, SomeNewActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
}
this
.startActivity(intent)

Intent.FLAG_ACTIVITY_MULTIPLE_TASK: It tells the system to always create a new task. This is equivalent of always.

val intent = Intent(this, SomeNewActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
}
this
.startActivity(intent)
  • Task can be either be in background state or foreground state. When the application is visible to the user, the task is in foreground state. When onStop is called i.e. user hits the home button or notification pops in or some other scenario…the entire task moves to background state.
    Note: The task is still in memory.
  • Say after hitting the home button…user opens Application B. The task for Application B comes in foreground state. Now, user gets back to Application A, so Application A task comes in foreground state (with preserved order of activities) while Application B’s task moves to background state.

If you’ve stayed this far please make sure to give a clap and hit the follow button.
Thank you and happy coding! 😄😄

Don’t know what to study next? or Unsure as to how learning this blog will help you…
Well, here’s a suggestion to go through the following blog to get the answers.

--

--

Harjot Singh

Android Developer @HealhifyMe I lose my handwritten notes often...so now making sure I can find my notes easily :p