Codementor Events

What are Tasks and Back Stack in Android...??

Published Apr 07, 2020
What are Tasks and Back Stack in Android...??

Have you ever wondered Whats happening behind the screnes when you tap an app icon and the app launches..??It creates a stack of Activities.The Activities are Arranged in a stack(The back Stack) in the order in which each activity is opened by the User.For example, an email app might have one activity to show a list of new messages. When the user selects a Watsaap messenger icon, a new activity opens to view that messages. This new activity is added to the back stack. If the user presses the Back button, that new activity is finished and popped off the stack.

Each Activity should be designed around a specific kind of action the user can perform and can start other activities.
from the best android developer guide.
Android defines the unit of a sequence of user interactions as Task. A Task is a collection of activities that user interact when performing a certain job.

behaviors of the Task and the Back Stack.

The application launcher makes another Task with the main Activity made and put in the foundation of the back stack (It has another job that we will Check it out later).

When the current Activity That we have created starts another Activity, then the new Activity is pushed on top of the stack and takes into focus.

The past Activity moves bellow the new Activity in the back stack and is Stopped. The system holds the present condition of this present Activity's UIs like content in the structure, scroll position and so on.

The Activities Keeps on piling the Back stack untill the back button is pressed

At the point when the Back Button is pressed then the present Activity is flown from the highest point of the back stack Hence it destroys the Activity and the past Activity resumes with its state reestablished.

The Back Button at that point continues popping the present Activities and reestablishing the past exercises. At the point when the last Activity is expelled from the back stack, at that point the Task ends to the screen that was last running before the making of the Task (for our situation the launcher screen or our main activity) Untill the back stack goes empty untill the task stopps existing And the Activities of different applications invoked by the intent are put into the same Task.

However the Task has few important properties:

The Task is brought to the foreground when the Launcher icon is pressed or when it is selected from the recent screens.

The Task Goes to the background when the home button is pressed or when a new task is created.

When the user Leaves the task for a long time or when he/she leaves mutiple Task in The Background,the memory is cleared in All Activities exept the root Activity for the system to recover the memory.

Using Android Manifest <activity> tag with attribute android:launchMode.
Including Flags in the intent delivered to the startActivity().
This launchModes and its equivalent startActivity Flags allow us to define, how a new instance of an Activity is associated with the current Task and specifies the instruction for its launch in the given Task.

launchMode — standard: This is the default method of the Activity. Right now, new occurrence of the Activity is made and placed in the Task which began it by steering the goal to it. The Activity right now be started up numerous occasions, each occurrence can have a place with an alternate errand and one undertaking can have its different examples.

launchMode — singleTop | banner — FLAG_ACTIVITY_SINGLE_TOP: This mode or banner delivers the very same conduct as the standard launchMode if the new Activity in not effectively present in the back stack as the top. On the off chance that it is available at the top, at that point it acts in an unexpected way. Right now, same Activity resumes with the call to its onNewIntentmethod.

launchMode — singleTask | banner — FLAG_ACTIVITY_NEW_TASK: If an Activity don't exist in a previously made Task, at that point it begins the Activity in another Task with Activity's new occurrence at the foundation of the Task's back stack, else the Task is presented with the Activity's last state reestablished and this Activity gets the new expectation in onNewIntent strategy. Just one example of the Activity can exist at once. Right now, Back catch is as yet ready to restore the client to the past Task's Activity.

launchMode — singleInstance: This launchMode is like the singleTask aside from that the System doesn't dispatch some other Activity into the Task holding the occasion. The Activity is consistently the single and the main individual from its Task. Any Activity began by this one opens in a different Task.

launchMode of singleTask and singleInstance are not suitable for the greater part of the applications.

banner — FLAG_ACTIVITY_CLEAR_TASK: This clears any current errand that would be related with the Activity before the Activity is begun. This Activity at that point turns into the new base of the assignment and old Activities are done. It must be utilized related to FLAG_ACTIVITY_NEW_TASK.

This particular flag is useful when the notification has to start the application by finishing the existing Activities. For example, if we want to start the SplashActivity from the Notification callback handler Service we use

Intent openIntent = new Intent(getApplicationContext(), SplashActivity.class);
openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(openIntent);

banner — FLAG_ACTIVITY_CLEAR_TOP: If the Activity being begun is now running in the present assignment then as opposed to propelling the new example of that Activity, the various exercises on it is demolished (with call to onDestroy strategy) and this goal is conveyed to the continued occasion of the Activity (presently on top), through onNewIntent technique.

Note: For FLAG_ACTIVITY_CLEAR_TOP, if the launchMode isn't characterized in the AndroidManifest or set as "standard" for the Activity then the Activity alongside its top is popped and another example of that Activity is set on the top. In this way, onNewIntent strategy isn't called.

This is unfortunate, more often than not we would need to reuse the Activity and invigorate it's view states like, the information in the rundown, with regards to the highest point of the back stack, as opposed to devastating and afterward reproducing it.

So as to accomplish this, we characterize the launchMode of the given Activity as singleTop and call the startActivity() with banner FLAG_ACTIVITY_CLEAR_TOP.

<activity
  android:name=".ActivityA"
  android:launchMode="singleTop"/>

Also

Intent intent = new Intent(context, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Precedence rule: If the current Activity adds the intent flag and the starting Activity has defined the launchMode then the current Activity’s request is honored.

oHistory: If this is set to true then it destroys the current Activity and removes it from the back stack whenever any other Activity is started by it. You don’t have to explicitly call finish for this activity. It is effective in SplashActivity.

clearTaskOnLaunch: If this is set to true then the back stack is cleared down to the root Activity whenever the user leaves the Task and return to it.

finishOnTaskLaunch: It is similar to clearTaskOnLaunch but operates on a single Activity.

NOTE.

Don’t use startActivityForResult for Activities within the same app, rather use singleTop and FLAG_ACTIVITY_CLEAR_TOP as described above to tackle the parent view update when child view is removed.

Investigating Tasks and back stacks for the Application at run time

Being developed stage, we ought to check the application Task and back stack, in the event that we have actualized the launchMode or have utilized the expectation Flags. This should be possible however the landfill of the action data.

Steps to follow for this specific examination:

Build and run the application in the Mobile phone or emulater through Android Studio.

Explore Few screens on the Mobile or Emulator.

Run adb shell dumpsys activity in the Studio's terminal.

This will give a long information content. Quest for the bundle name of your application.

Under the area ACTIVITY MANAGER ACTIVITIES we can discover all the data about the assignment and exercises in the back stack.

Thank you so much As we Advance in Android Development Course Please Help me by giving me A feedback in the comments section on how i will improve.

Discover and read more posts from Dancan Yego
get started
post commentsBe the first to share your opinion
Show more replies