Task and Back Stack in Android
When doing a job, users engage with a task, which is a set of actions. The activities are stacked in the order in which they are opened in a stack called the back stack. One action in an email app, for example, maybe to display a list of fresh messages. When the user picks a message, a new activity appears in which the user may read the message. This new action has been sent to the back of the queue. When the user clicks the Back button, the new action is completed and removed from the stack. When numerous applications are running in a multi-windowed environment, which is allowed in Android 7.0 (API level 24) and above, the system maintains tasks for each window individually; each window may contain several tasks. The system organizes tasks, or groups of tasks, on a per-window basis for Android apps running on Chromebooks.
For most tasks, the device’s Home screen is the starting point. The work of an app is brought to the front when the user taps an icon in the app launcher (or a shortcut on the Home screen). If no task for the app exists (because it hasn’t been used recently), a new task is created, and the app’s “main” activity is opened as the stack’s root activity.
When the current activity switches to a new one, the new activity is pushed to the top of the stack and takes control of the attention. The preceding action is still there in the stack, but it is no longer active. When a task is completed, the system saves the current state of the user interface. The current activity is plucked off the top of the stack (the activity is destroyed) when the user hits the Back button, and the prior activity restarts (the previous state of its UI is restored). The activities in the stack are only pushed onto and popped off the stack—pushed into the stack when the current activity starts it and popped off when the user exits it using the Back button. As a result, the back stack is an object structure that is “last in, first out.” Figure 1 depicts this behavior using a timeline that shows the progression of activities as well as the current back stack at each point in time.
What Happens on Back Press?
If the user presses Back repeatedly, each activity in the stack is popped off to show the one before it, until the user returns to the Home screen (or to whichever activity was running when the task began). The task is no longer active after all actions have been removed from the stack.
A task is a logical unit that may be sent to the “background” when users start a new task or press the Home button to return to the Home screen. All operations in the task are paused while it is in the background, but the task’s back stack remains intact—the task has just lost focus while another task is being performed, as seen in figure 2. A task can then be brought back into the “foreground,” allowing users to resume their work where they left off. Assume that the current task (Task A) contains three activities in its stack, two of which are underneath the current activity. The user hits the Home button, then opens the app launcher and selects a new app. Task A is pushed to the background when the Home screen appears. When a new app is launched, the system creates a task (Task B) for it, which has its own set of activities. After interacting with that app, the user goes back to Home and picks the app that launched Task A in the first place. Task A now appears in the forefront, with all three activities in its stack intact and the activity at the top of the stack resumed. The user may now return to Task B by navigating to Home and choosing the app icon that initiated the task (or choosing the app’s task from the Recents page). On Android, this is an example of multitasking.
Because the activities in the back stack are never reorganized if your app allows users to launch a specific activity from multiple activities, a new instance of that activity is produced and placed onto the stack (rather than bringing any previous instance of the activity to the top). As a result, a single activity in your app may be invoked many times (even from distinct jobs). As a result, if the user uses the Back button to browse backward, each instance of the activity will be presented in the sequence in which it was accessed (each with its own UI state). If you don’t want an activity to be created more than once, however, you may change this behavior. In the section on Managing Tasks, we’ll go through how to accomplish that. To describe the default behavior for activities and tasks, consider the following:
- Activity A is interrupted when Activity B begins, but the system’s state is preserved (such as scroll position and text entered into forms). When the user returns to Activity A after pressing the Back button in Activity B, the state of Activity A is restored.
- When a user exits a task by hitting the Home button, the present activity is terminated and the task is placed in the background. Every activity in the task is saved in the system’s memory. The task comes to the foreground and resumes the activity at the top of the stack if the user subsequently continues it by clicking the launcher icon that started it in the first place.
- The current activity is removed from the stack and deleted when the user hits the Back button. In the stack, the prior action is resumed. The system does not keep track of the status of activity when it is deleted.
Even from other tasks, activities can be instantiated several times.
Organizing Your Tasks
For most apps, the way Android manages tasks and the back stack (by placing all activities initiated in sequence in the same task and in a “last in, first out” stack) works fine, and you shouldn’t have to worry about how your activities are connected with tasks or how they appear in the back stack. You may, however, decide that you want to deviate from the standard. Perhaps you want an activity in your app to start a new task (rather than being placed within the current task), or perhaps you want to bring an existing instance of activity forward (rather than creating a new instance on top of the back stack) when the user leaves the task, or perhaps you want your back stack to be cleared of all activities except the root activity when the user leaves the task. With characteristics in the activity> manifest element and flags in the intent that you send to startActivity, you can perform all of these things and more.
The following tags can be used for defining the Back Stack forcefully:
GeekTip: When it comes to activities and tasks, most apps should not interfere with the usual behavior. If you decide that changing the default behaviors is important for your activity, proceed with caution and make sure to evaluate the usability of the activity during launch and while returning to it using the Back button from other activities and tasks. Be careful to check for navigational behaviors that are inconsistent with the user’s expectations.
Few Key Characteristics of a Task
When a new task is created or the Home button is clicked, it disappears into the background. The job is then brought to the front by clicking the launcher icon (this is the other function of the launcher icon that we discussed before) or selecting it from the recent screens. When many tasks are running in the background or the user exits a Task for an extended period of time, the system clears the task of all activities except the root Activity in order to free up memory. Only the root Activity is restored when the user returns to the Task again
A Quick Shot Example
The Android Browser application specifies that the web browser activity should always be launched in its own Task. This implies that if your app sends out an intent to launch the Android browser, that activity isn’t assigned to the same task as your app. Instead, a new task is started for the Browser, or an existing task is moved forward to fulfill the new intent if the Browser currently has one running in the background.
How to add Different launchModes using Java:
And that’s it!
That’s how you master the act of Backstack and Tasks!
Please Login to comment...