3.12 Android Tasks


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

                   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.

    Bringing theAndroid task 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

                                                         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/>  tag.

         • In case of intent filter flags, when we call startActivity(). Flags are specified with intents for manipulating their behavior. In other words, behavior of activities would be specified with intent flags. Manifest

    In manifest file under tag we have an attribute called launchMode. This mode specifies the way activity should launch in the task. There are four types of launch and they are:

    1. “standard” (default mode): A new instance is created in task when activity was started and the intent is routed to it. An activity can be instantiated many a time and each instance is allowed to belong to different task. It is also possible for a single task to have multiple instances.

    2. “singleTop”: If an instance is already present then intent is routed to that instance and new instance creation is avoided in such case by making a call to onNewIntent(). An activity can be instantiated many a times and each instance is allowed to belong to different task. It is also possible for a single task to have multiple instances.

    3. “singleTask”: System creates a new task and instantiates the activity at root or main activity of task. If an instance already exists then intent is routed to that instance by calling the method onNewIntent(). In short, there can be only one instance of an activity at a time.

    4. “singleInstance”: There is only one instance of one activity in a particular task. In short, one instance,one activity and one task. Intent-filter flags

    With following flags default behavior of task can be altered and they are: 1. FLAG_ACTIVITY_NEW_TASK: Activity is started in a new task. If an instance already exists then intent is delivered to that instance with onNewIntent() call. It is similar to “singleTask” launchMode attribute.

    2. FLAG_ACTIVTITY_SINGLE_TOP: Started activity is the current activity then its instance receives call to onNewIntent(). Current activity implies the activity at top of the stack. New instance is not created. It behaves ina similar way to “singleTop” mode.

    3. FLAG_ACTIVITY_CLEAR_TOP: If started activity is present in current task then all the activities above it on stack top are destroyed and it is moved up to the stack top.

    3.12.3 Start an Android  task

    An activity can be set as the starting point of a task by giving an intent-filter with “android.intent.action.MAIN” as action and category as “android.intent.category.LAUNCHER”. Looks similar, isn’t it. We have used these attributes without this knowledge. This type of intent-filter creates an icon and label which is visible to user as it is displayed on application launcher. Users can launch activity and return to the task every time and any time. We should be very careful while using “singleTask” and “singleInstance”. They should be used only when the intent filter is qualified with above mentioned action and category respectively otherwise, user will not be able to re-work on task after pressing the home button. He/ she will never be able to return to the task as pressing home button would push it to background and remain invisible there for ever after.

           android:label="@string/app_name" >    
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />

                         Figure - Example of intent-filter from Android HelloWorld App

    3.12.4 Cleaning Android Back Stack

    After a certain interval of time except the root activity or main activity everything else is destroyed in back stack. This is the default behavior of Android back stack. This can be modified by following attributes of activity and they are:

    • 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.

    So ladies and gentleman, boys and girls we are done with the chapter Three. This was the last session for the longest unit of our tutorial. Thanks for your patience and congratulations on completion! Keep practicing. See you in next chapter. Till then, Happy App Developing!!!