Android Check trap--activity life cycle and startup mode

Source: Internet
Author: User

First, life cycle
    • OnCreate (): Callback when activity is first created when activity is started.
    • Onrestart (): Callback when activity is started again.
    • OnStart (): A callback after OnCreate () is initiated when the activity is first started, and is called back after Onrestart () when it is started again. The activity is now visible but does not appear until the foreground cannot interact with the user.
    • Onresume (): Callback after OnStart (), at which time the activity is fully visible and can interact with the user.
    • OnPause (): When the user returns or jumps to the next activity, this method is recalled, indicating that the current activity is being stopped, but the activity is still visible, and the OnStop () is usually immediately recalled.
    • OnStop (): Indicates that activity is about to stop.
    • OnDestroy (): Indicates that activity was destroyed.

Notable places:

    1. OnStart and Onresume look similar, but there are some subtle differences, onstart that activity although it is now visible but not fully displayed to the foreground, Onresume said at this point has been fully displayed to the foreground, and can interact with the user, The difference between OnPause and OnStop is similar to this.
    2. When Activitya starts activityb, their life cycle methods are executed in the following order:
      A.onpause->b.oncreate->b.onstart->b.onresume->a.onstop

The logs are as follows:

1. The activity life cycle method execution order under normal circumstances:
    • Start a activity:oncreate ()->onstart ()->onresume ();
    • When the current activity jumps to the next page (or when the user switches to the background), the current activity is paused: OnPause ()->onstop ();
    • Then return to this activity: Onrestart ()->onstart ()->onresume ();
    • When activity exits: OnPause ()->onstop ()->ondestroy ().
2, the Activity life cycle method execution order when the screen is switched:
    • OnPause ()->onsaveinstancestate ()->onstop ()->ondestroy ()->oncreate ()->onstart () Onrestoreinstancestate ()->onresume ().

The activity is destroyed first, then recreated, and OnPause () is called after the Destroy () is called onsaveinstancestate (bundle bundle) when the screen is switched. At this point we have the opportunity to write some information that needs to be restored to the bundle parameter of Onsaveinstancestate (), and call OnStart () after the activity is rebuilt, calling Onrestoreinstancestate ( Bundle bundle), at which point we can use Onrestoreinstancestate () to restore some of the necessary information, and its parameters are the parameters we write in Onsaveinstancestate.

3. The life cycle method execution order when low priority activity is reclaimed due to insufficient system memory

This situation and the second type of the screen switch when the situation is not much, will also be executed onsaveinstancestate () and Onrestoreinstancestate ().

Two or four startup modes
    • Standard: The default startup mode, activity can be instantiated multiple times, that is, the same stack can exist multiple instances of activity.

If you use ApplicationContext to start the activity in standard mode, you will get an error, because this mode activity defaults to the task stack to which the activity that started it Not the activity type of context and there is no task stack. The solution is to specify Flag_activity_new_task when you start activity with ApplicationContext, which creates a new stack for it when you start the activity.

    • Singletop: Stack top multiplexing mode. Activity initiated in Singletop mode if there is already an example of the = = stack top = =, then the activity will be re-used when it is started and the Onnewintent () method is called. If not at the top of the stack, a new example is created, with the same effect as standard.

    • Singletask: In-stack multiplexing mode. This mode allows only one instance of the activity to exist in a stack, and when the activity of this mode is started, a new instance is created if there are no instances of the activity in the stack. If the instance already exists in the stack, all activity on top of it is destroyed, and the activity instance is eventually placed on top of the stack, and the Onnewintent () method is recalled.

    • SingleInstance: Single-instance mode. The activity of this mode is only opened in a separate task stack, and there is only one instance of this new task stack. When the activity instance is started again, the existing tasks and instances are reused (and called onnewintent). Other activity initiated by the activity is automatically run with another task stack.

The difference between singleinstance and Singletask:

    • SingleInstance at the same time, there will only be one instance in the system, while the activity of the Singletask mode can have multiple instances, as long as these instances are in different task stacks.
    • For example, when application a initiates a singleinstance activity, and application B initiates the activity, the instance that has already been started is reused.

Finally want to say is, this series for Bo master on Android knowledge again comb, check the learning process, on the one hand is forgetting things to review again, on the other hand believe in the process of re-learning will have a huge new harvest, if you also have with me the same idea, may wish to pay attention to my study together , explore each other and make progress together!

Reference documents:

    • Explore the art of Android development
    • "Android Development advanced from small to expert"

Android Check trap--activity life cycle and startup mode

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.