A task is a group of actions that users take when attempting to complete a task in your app. These exercises are organized in a stack called the back stack in the order that they are opened. An email app, for instance, might contain an activity that displays a list of fresh messages.
A new activity opens to allow the user to view the message they selected. The back stack now includes this new task. The user can then finish and remove the new activity from the stack by pressing or gesturing Back.
Lifecycle of A Task and Its Back Stack
The gadget The home screen is where most tasks begin. The task for that app is brought to the front when a user presses its icon in the app launcher or on the Home screen. The main activity for the app launches as the root activity in the stack if no task already exists for it (since the app hasn’t been used recently).
The new activity is moved to the top of the stack and takes focus when the current activity starts another. Although halted, the prior action is still present in the stack. The system keeps its user interface in the state it is in when an activity is stopped.

The current activity gets popped off the top of the stack (destroyed) and the prior activity picks back up when the user performs the back action (the previous state of its UI is restored).
Activities on the stack are only ever pushed onto it when they are initiated by the current activity and popped off when the user uses the Back button or gesture to exit it. The back stack functions as a last in, first out object structure as a result. Figure 1 illustrates this pattern with a timeline that displays the progression of various tasks as well as the size of the backlog at each point in time.
The Behavior of The Back Button During Root Launcher Operations
Activities that specify an Intent filter with both ACTION MAIN and CATEGORY LAUNCHER are known as root launcher activities. These tasks are distinct from others since they serve as entry points into your app via the app launcher.
Depending on the version of Android that the device is running, the system responds differently when a user pushes or gestures Back from a root launcher activity.
Read More: How to Make an IPhone Call Recorder.
Activities in The Foreground and Background
When a user starts a new task or navigates to the Home screen, a task, which is a cohesive unit, may migrate to the background. The task has merely lost focus while another task is being performed in the background because all of its operations have halted, but the task’s back stack is still intact, as seen in figure 2. The task can then come back into focus so users can continue where they left off.
Take a look at the task flow below for a current task (Task A), which has three activities in its stack, two of which are underneath the current activity:

- The user launches a new app from the app launcher after using the Home button or gesture.
- Task A is put in the background when the Home screen appears. The system launches Task B, an app-specific task with its own stack of activities when a new app first launches.
- The user returns to Home after interacting with that app, where they choose the app that launched Task A in the first place.
The activity at the top of the stack resumes, and Task A now takes center stage. All three activities in its stack are still active. The user can now return to Task B by navigating to the Home screen and choosing the app icon that launched it (or by choosing the task from the Recents screen).
Read More: Best Ways to Fix Blue Screen of Death Windows 10
Several Instances of Activity
If your app allows users to launch a specific activity from more than one activity, a new instance of that activity is created and put onto the stack because the activities in the back stack are never rearranged (rather than bringing any previous instance of the activity to the top).

As a result, one activity in your app may be instantiated more than once (even from other tasks), as seen in figure 3. Each instance of the activity is revealed in the order they were opened if the user navigates backward using the Back button or gesture (each with their own UI state). If you do not want an activity to be instantiated more than once, you can change this behavior. The section on managing tasks that follow discusses how to go about doing this.