[Android] task and back Stack

Source: Internet
Author: User
Tags home screen
Document directory
  • Basic Concepts
  • Foreground task and background task
  • Multiple instances of activity
  • Manage tasks and back stacks
Http://coolxing.iteye.com/blog/1279606basic concepts

A task is a collection of activities. These activities are usually started for the same purpose. The system organizes these activities in the stack in the order they are created (the so-called back stack ). each task has a back stack that only belongs to it.

For example, in a contact app, when creating a contact, you must specify an avatar for the contact. in this case, the image browser will start to select an image as the Avatar, and switch back to the contact app after the selection is complete. in this process, at least two activities will be started, and the system will arrange these two activities in the same task, it seems like these two activities belong to the same app.

Home screen is the place where most tasks are created. when you click the app icon of home screen, a task is usually created and the main activity of the app is used as the root element in the back stack. if the current activity starts another activity, the new activity will be pushed into the back stack and get the focus. the previous activity is still in the back stack, but in the stopped status. A stopped activity contains its status data in the memory.

If you click the back key, the current activity will pop up from the back stack and be destroyed by the system (the ondestroy () method is called ), the previous activity in the stack becomes the new top element of the stack and gets the focus (its status data will not be lost ).

The activity in the stack does not have the opportunity to re-sort. Back stack simply presses in Or pops up the activity. When the current activity starts a new activity, it pushes the new activity into the stack, the current activity is displayed when you click back.

If you keep clicking the back key, the activity in the back stack will pop up from the stack until you return to the home screen. when all the activities in the stack are out of the stack, the back stack and task will no longer exist.

Is a simple example:

Foreground task and background task

Tasks can be divided into foreground tasks and background tasks. when you press the Home key, the current task changes from foreground task to background task. if a task changes to a background task, all the activities in the stack will be in the stopped state. A task can also be recovered from a background task to a foreground task. For example, you can click the activity icon of a task root element again on the home screen interface. two tasks are demonstrated: if the user clicks the picture of activity1 again, task a becomes a foreground task again, And activity3 in task a returns to the running status.

All the activities in the background task are in the stopped state, while other activities in the foreground task are in the stopped state, except for the activity on the top of the stack, the system may destroy the background activity due to insufficient memory, which may cause data loss. for more information about how to prevent data loss, see my other blog http://coolxing.iteye.com/blog/1279447. if a background activity is destroyed by the system, its position in the stack is retained. When you press the back key to locate the destroyed activity, the activity will be rebuilt.

Multiple instances of activity

An activity may be instantiated multiple times. The instantiated objects of these activities may exist in the same task or in different tasks.

Homeactivity is used for two instances, and these two instances exist in the same task.

If you do not want an activity to have multiple instance objects, you can configure them in the androidmanifest. xml file or use the flag attribute of intent.

Manage tasks and back stacks

Tasks and back stacks automatically managed by the system can usually work well and developers should not intervene. if you need to intervene in task and back stack management for some special considerations, Android also provides a corresponding way: androidmanifest. the <activity> label of the XML file sets the corresponding attributes. You can also set the flag attribute for the intent that starts the activity. if both are set, the flag attribute of intent prevails.

Set Launch mode in the manifest File

<Activity> the launchmode attribute value of the tag may be:

1. "standard", the default value of the launchmode attribute. after the activity is started, it is pushed into the current back stack. The activity can have multiple instances, which can be distributed in different tasks or in the same task.

2. "singletop ". it indicates that when the activity is started, if the activity is already the top activity of the stack of the current task, no new activity will be created, and the other situations are the same as "standard ". for example, a task contains four activities, which are A-B-C-D, Activity D is at the top of the back stack, and the launchmode attribute of D is "singletop ". if Activity D is started again at this time, because Activity D is already at the top of the stack, the instance of Activity D will not be created again, the activity in the current back stack is still A-B-C-D. however, if the launchmode of D is "standard", the new activity D instance will be created again and pushed into the stack, the activity in the back stack will change to A-B-C-D-D.

3. "singletask ". indicates that when the activity is started, if the activity does not have instances in any task, a new task will be created, and the activity will be pushed into the new task. if an instance object of the activity already exists in a task, the activity will not be created, but the task where the activity is located will become a foreground task.

4. "singleinstance". Similar to "singletask", the difference is that the task where the "singleinstance" activity is located cannot own other activities.

Set flag attribute of intent

The startactivity () method requires an intent to start an activity. You can set the flag attribute of the intent to specify the optional values of the launchmode. Intent flag attribute of the activity to be started:

1. flag_activity_new_task. The launchmode with the corresponding value being "singletask.

2. flag_activity_single_top. launchmode with the corresponding value "singletop.

3. flag_activity_clear_top. there is no corresponding value in launchmode. indicates that if the activity to be started is already in the back stack of the current task, the Instance Object of the activity will not be created and all the activities located before the activity in the stack will be destroyed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.