Development of Android application: Back stacks and tasks
Android is an operating system, normally used in touch screen mobile phone devices powered by Google. Linux based operating system is an open-source platform, which is helpful for developers to understand the interface, framework and enable them to create an application for android phones. The library of Android application is stored at Google and known as Google Play store, where a user can get free and buy a paid application and run it to the device. The main benefit of android phones, a user has a different application which runs for communication, weather forecasting, and games, etc
The development process for an android device is not like other development, a few tools are used in android development like Eclipse, Install the ADT plugin for Eclipse Android SDK (Software development Kit). Android is an open-source operating system, so it’s very easy to develop an android application by using Eclipse on Mac, and Windows operating systems.
Advantages Android offers
- There is no need to pay for new hardware to develop an android application because The Android SDK is available for Windows, Mac and Linux,
- An SDK built on Java. If you’re familiar with the Java programming language, you’re already done the most of its development.
- By distributing your application on Android Market, it’s available to hundreds of thousands of users instantly. You’re not just limited to one store, because there are alternatives, too. For instance, you can launch your application on your own web page. Amazon have recently been supposed to be preparing their own Android app store also.
- The technical SDK documentation, new resources are being published for Android developers as the platform gains popularity among both users and developers.
Android Task and Back Stack Guide and tips
Android Activities are the logical concept of the display screens that we want a user to navigate through. The relation that every Activity holds concerning others is very important for a good user experience. This relation should be designed with the focus of developing an uncomplicated and pattern forming scheme from the user’s perspective.
One of the most major features of achieving the above is to design a proper forward and backward navigation. The android design has already taken great care to provide smooth user experience in the management of screen flows.
Android developer Experts says: Every Activity should be designed around a specific type of action the user can perform and can start other activities.
Let’s review the Android’s defaulting implementations in the above respects.
Android describes the unit of a sequence of user interactions as Task. A Task is a collection of activities that users interrelate when performing a certain job.
The Task holds the Activities in a Stack called Back Stack. The Stack has a LIFO structure and stores the activities to open them. The Stack Activities are never rearranged. Navigation of the back stack is done using the Back button.
We will now go through the default behavior of the Task and the Back Stack.
1. The application launcher generates a new Task with the main Activity created and placed in the root of the back stack.
2. When an existing Activity starts another Activity, then the new Activity is pushed on top of the stack and takes focus.
3. The preceding Activity moves below this new Activity in the back stack and is stopped. The system keeps the present state of this Activity’s user interfaces like scroll position, text in the form, etc.
4. The following new Activities keep on piling the back stack.
5. When the Back button is pushed then the recent Activity is popped from the top of the back stack. This finishes the Activity and the previous Activity resumes with its state restored.
6. The Back button will then continue to pop up the current Activities and restore the previous activities. When the last Activity is removed from the back stack, then the Task ends on the screen that was last running before the creation of the Task.
7. When the back stack goes blank then the Task stopped to exist.
8. Activities of different applications appealed by the intent are put into the same Task.
Task has a few important properties:
1. It goes into the background when a new task is produced or when the Home button is pushed.
2. It is then brought to the foreground by clicking the launcher icon or by selecting the task from the current screens.
3. When the user leaves the Task for a long time or when multiple tasks are in the background, the system to recover memory clears the Task of all the Activities excluding the root Activity. When the user returns to the Task again, only the Activity of root is restored.
An Important Result:
We can derive from the above text a very important result. If an Activity is started from multiple Activities, then a new instance of that Activity is produced and pushed on the stack rather than bringing the previous instance of that Activity to the top. In this case, the Back button exposes the instance of similar Activity multiple times when its order status has been created.
This certain behavior is unattractive and the main concern of this article is learning to control the back stack so that an Activity happens in a single instance at a time and with relation to its originator Activity. We’re going to see how to do that in the text to follow.
There are some situations where we would want the management and control the Task as a deviation from the default behavior. The Android Developer Guide lists some of those situations as follows:
• When starting a new Activity, it is necessary to start in a new Task instead of being placed in the back stack of the existing Task.
As an example, the Android Browser application states that the Activity of the web browser should always open in its Task. This means that if your application problems and intent to open the Android Browser; its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task will be brought forward to handle a new intent.
• When starting a new Activity the current instance is necessary to be brought from the back stack as we already mentioned.
• The back stack should be cleared to the root Activity when the user leaves the current Activity.
The launch Modes and its corresponding start Activity Flags enable us to define, how a new instance of an Activity is related with the existing Task and specifies the instruction for its launch in the given Task.
The types of launch Modes and its equivalent start Activity Flags are presented and investigated in the following points:
• launch Mode — standard: This is the default mode of the Activity. In this mode, a new instance of the Activity is generated and put in the Task which started it by routing the intent to it. The Activity in this mode can be instantiated several times, each instance can belong to a different task and one task can have its multiple instances.
• launch Mode — single Top | flag — FLAG_ACTIVITY_SINGLE_TOP: This mode or flag creates the same behavior as the standard launch Mode if the new Activity is not already present in the back stack as the top. If it’s present at the top then it acts differently. In this case, the same Activity resumes with the call to its on new intent technique.
• launch Mode — single Task | flag — FLAG_ACTIVITY_NEW_TASK: If an Activity does not exist in a task that has already been established, the Activity must start in a new task with the current instance of the Activity at the root of the back stack of the task, else the Task is brought forward with the Activity’s last state restored and this Activity receives the new intent in on New Intent technique. There can only be one instance of the activity at a time. In this case, the Back button is still capable to return the user to the previous Task’s Activity.
• Launch Mode — single Instance: This launch Mode is related to the single Task except that the System does not launch any other Activity into the Task holding the instance. The Activity is constantly the single and the only member of its Task. Any Activity started by this one will be opened in a separate Task.
Launch Mode of single Instance and single Tasks are not appropriate for most of the applications.
• flag — FLAG_ACTIVITY_CLEAR_TASK: This will clear up any current task that would be associated with the Activity before the Activity is started. This Activity then becomes the new root of the task and old Activities are finished. It can only be used in conjunction with new tasks for flag activity.
This particular flag is useful when the notification has to start the application with the completion of existing Activities. For example, if we want to start the Splash Activity from the Notification callback handler Service.
• flag — FLAG_ACTIVITY_CLEAR_TOP: If the Activity being started is already running in the existing task then instead of starting a new instance of that activity, all the other activities on top of it are demolished and this intent is provided to the resumed instance of the Activity, through on new intent method.
Note: For FLAG_ACTIVITY_CLEAR_TOP, if the launch Mode is not defined in the Android Manifest or set as ‘standard’ for the Activity then the Activity along with its top is popped and a new instance of that Activity is placed on the top. So, on new intent technique is not called.
This is undesirable, most of the time we would want to reuse the Activity and refresh its view states like, the data in the list, when it comes to the top of the back stack, rather than destroying and then recreating it.
Steps to follow for this particular investigation:
1. Navigate a few screens on the Mobile.
2. Build and run the mobile app through Android Studio.
3. Run the ADB shell dumpsys activity on the Studio’s terminal.
4. under the section activity manager activates we can find all the information about the activities and tasks in the back stack. 5.This is going to give a long info text. Search for the name of the package of your application.