Android Elite Biography Review--eighth: Activity and activity Call stack analysis

Source: Internet
Author: User

  • 8.1 Activity
    • 8.1.1 Origins
    • 8.1.2 Activity Patterns
    • 8.1.3 Life cycle
  • about 8.2 activity task Stack
  • 8.3 activitymanifest Boot mode
    • 8.3.1 Standard
    • 8.3.2 Singletop
    • 8.3.3 Singletask
    • 8.3.4 singleinstance
  • 8.4 Intent Flag Boot Mode
  • 8.5 emptying the task stack
  • 8.6 Activity Task Stack usage

The most frequently occurring components in the four components

Activity is the first interface to interact with the user, it provides a user to complete the instruction window, the system uses the activity stack to manage the activity

  • Activity/running
    Activity is at the top level of the activity stack, visible, and interacting with the user
  • Paused
    When the activity loses focus and is placed at the top of the stack by a new non-full-screen activity or a transparent activity, the activity is converted into a paused form, but it simply loses the ability to interact with the user, all state information, and member variables remain, Only in the case of the system memory polar, will it be reclaimed by the system
  • Stopped
    If an activity is completely covered by another activity, the activity enters the stop pattern, where he is no longer visible, but still maintains all state information and member variables
  • Killed
    Activity is in a killed state when the activity is recycled by the system or the activity has never been created

Google Classic life cycle chart:

Due to the not detailed introduction of Android Elite, I use the Android development art to explore this book to introduce the life cycle:

    • OnCreate: Indicates that activity is being created, which is the first method of life cycle, where you can do some initialization work, such as calling Setcontentview to load the interface layout resources, initializing the data needed for the activity, etc.
    • Onrestart: Indicates that the activity is restarting, in general, when the current activity is never visible and becomes visible again, Onrestart is called, which is usually caused by user behavior. For example, the user presses the home button to switch to the desktop or the user opens a new activity, this is the current activity will be suspended, that is, OnPause and OnStop is executed, and then the user back to the activity, this will happen
    • OnStart: The activity is being started, is about to start, then the activity is already visible, but has not appeared in the foreground, also cannot interact with the user, this time actually can understand as the activity already showed, but we still cannot see
    • Onresume: The activity is already visible, and appears in the foreground and starts the activity, pay attention to this and onstart contrast, onstart and onresume all indicate activity is already visible, But onstart when the activity is still in the background, onresume when the activity is displayed in the foreground
    • OnPause: Indicates that activity is stopping, normally, immediately after OnStop will be called, in special cases, if this time quickly back to the current activity, then Onresume will be called, the author's understanding is that this situation is an extreme situation, User operation is difficult to reproduce the scene, at this time can do some storage data, stop animation and other work, but attention can not be too time-consuming, because it will affect the new activity display, OnPause must be executed first, the new activity Onresume will execute
    • OnStop: The activity is about to stop, can do a bit of heavy recycling work, also not too time-consuming
    • OnDestroy: Indicates that activity is about to be destroyed, which is the last callback in the activity life cycle, where we can do some recycling work and eventually release the resources

In addition to the life cycle, the state of the activity is preserved:

  • If you are in a stopped state for a long time and the system needs more memory or the system memory is extremely tense, your activity will be recycled, and the system will pass the activity state through Onsaveinstancestate () in order to compensate you. method is saved to the bundle object, and when you need to recreate the activity, the saved bundle object is passed to the activity's Onrestoreinstancestate () method and the OnCreate () method, The method can be called to restore the state when it was destroyed.
  • When an app launches, if the app's task stack does not exist in the current environment, then the system creates a task stack, and thereafter the activity that the app launches is managed in this task stack, which is also called a task, which represents a collection of several activity Together they form a task, especially to note that the activity in a task can come from different apps, and the activity of the same app may not be in a task

  • The structure of the stack is a last-in-first-out linear table, set by the attribute Android:launchmode in the Androidmanifest file or by the intent flag.

The manifest offers four startup modes:

  • Standard
  • Singletop
  • Singletask
  • SingleInstance

For these four startup modes can be easily understood in my other blog: four startup modes

  • intent.flag-activity-new-task: Starts an ACTIVITY with a new task, but each aetivity that is started will be in a new task, which is typically used in the Serv Actiity scenario launched in ice, because the activity stack does not exist in the service, use that flag to create a new activity stack and create a new activity instance
  • Flag-activity-single-top: Use Singletop mode to start an ACTIVITY that is the same as the android:launchmode= "singletop" effect specified
  • Flag-activity-clear-top: Use Singletask mode to start an ACTIVITY that is the same as the android:launchmode= "Singletask" effect specified
  • Flag-activity-no-history: Using this mode to start acuvity, when the activity starts another acuvity, the activity disappears and is not persisted in the activity stack, such as a-b,b in this mode to start C , C again starts D, the current activity stack is ABD

The system also provides a way to clear the task stack so that we can purge a task, usually in the Androidmanifest <activity> tag using the following properties to clean up:

  • Cleartaskonlaunch: Each time the activity is returned, all activity on the activity is cleared, and the IELTS basis allows the task to be initialized with only one activity at a time.
  • Finishtaskonlaunch: This property and cleartaskonlaunch a bit similar, but cleartaskonlaunch effect on others, and finishtaskonlaunch role in themselves, through this attribute, When you leave the task where the activity is located, the activity will be erased when the user returns.
  • Alwaysretaintaskstate:task a gold medal, if the activity this property is set to True, then the activity's task will not accept any purge command, always maintain the state of the current task

No knowledge points

Android Elite Biography Review--eighth: Activity and activity Call stack analysis

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