Recipe Forcing Single Task Mode

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:


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

"onPause", Toast.LENGTH_SHORT).show();

returns to the task in its last state, specify the following in the activity element of the root activity of a task:


Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment