Sunday, 1 February 2015

Forcing Single Task Mode

12:44:00 Posted by Kumanan
As an application is navigated away from and launched again, it can lead to multiple
instances of the activity on the device. Eventually the redundant instance of the activity is
killed to free up memory, but in the meantime, it can lead to odd situations.To avoid
these, the developer can control this behavior for each activity in the AndroidManifest.

To ensure only one instance of the activity runs on the device, specify the following in
an activity element that has the MAIN and LAUNCHER intent filters:

android:launchMode="singleInstance"

This keeps a single instance of each activity in a task at all times. In addition, any child
activity is launched as its own task.To constrain even further to only have a single task for
all activities of an application, use the following:

android:launchMode="singleTask"

This allows the activities to share information easily as the same task.

In addition, it might be desirable to retain the task state, regardless of how a user navigates
to the activity. For example, if a user leaves the application and relaunches it later,
the default behavior often resets the task to its initial state.To ensure the user always returns to the task in its last state, specify the following in the activity element of the
root activity of a task:

android:alwaysRetainTaskState="true"