3.12.1 Introduction to Android tasks
• An Android task is a collection of activities while performing a particular work.
• These are the activities with which users interact in order to complete a job
• Initial chapters covered a topic called Back Stack. These activities are arranged in this stack in a sequence.
Figure - Activities are arranged sequentially in back stack
• This sequence is a series of activities required to complete the action
• Home screen of our device is starting point of most of the tasks.
• When application launcher is touched, application’s task comes to the foreground.
Figure - Bringing the Android task to the foreground
If the application was not used recently then there would be no task corresponding to it. In that case, a new task is created. Main activity of the application opens as the base or root activity in stack.
Figure - New Android task is created
- When current activity starts, it takes the focus while previous activity is pushed to back stack. It is stopped. In this case, system retains the current state of stopped activity.
- Activities are pushed or popped. There is no rearranging of activities inside stack.
- When user presses back button, activity is popped out from stack. When current activity starts another activity it is pushed in to stack. Hence, “Last In, First Out” or “LIFO” structure.
- Tasks act like fevicol. They act like cohesive units. They can move to background when a new task comes to foreground. Background tasks have stopped activities but the back stack for task remains as it is, only focus is lost. As focus is captured by the foreground task. As soon as foreground task is done, background task can come to foreground
- Multiple tasks can co-exist in background. If device runs many background tasks then system may destroy activities as and when required
- Let us consider two activities namely X and Y respectively. Let us assume Activity X as current activity and it started another activity Y. Now activity Y is at foreground. At this stage, Activity X with all of its resumed state is preserved in back stack. Now if we press back button while activity Y is running, activity X will resume from the point of action it was stopped.
- There is no rearrangement strategy in back stack. Suppose Activity X is at foreground and starts an activity which is not at the top of stack then another instance would be created for this purpose and previous is not going to come before other activities present above it in the stack. Hence multiple instances of activities can be instantiated from different tasks
• onSaveInstanceState():Stopped activities preserves their state in back stack. But system may destroy such an activity when it is in need of resources. In that case, we should call this method so that activity state is not lost. Even if system dares to destroy, it would remember that when activity would come to top of the stack at that time I must resume the activity state where it was left.
3.12.2 Android Task Management
In general their management should be default and order should not be disturbed. Anyhow, if it is mandatory to do so then it can be done by two approaches:
1. By manifest file
2. By intent filter flags
• In case of manifest file, when we declare a new activity we can specify the way it should adopt to associate with the task when it starts by using <activity/>
188.8.131.52 Manifest 184.108.40.206 Intent-filter flags 3.12.3 Start an Android task
<activity android:name="com.android.tution.Hello.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
3.12.4 Cleaning Android Back Stack
- alwaysRetainTaskState: As the name suggests, this attribute, if set to true, allows the system to retain all the activity states as it was even after a long period of time gap.
- clearTaskOnLaunch: In this case, if set to true, this attribute would loose the current state of activity and only the initial state of the task is preserved after user returns. Even if user leaves task for one moment, he/she has to re-work from initial state of root activity. This is in contrast to alwaysRetainTaskState.
- finishOnTaskLaunch: This attribute, if set to true, would cause any one activity to go away and not the entire stack. Activity may be root activity also.Only for the present session task would be alive. If user leaves and returns back he/she would find nothing about this particular activity of task.