Android Art--a deep look at the life cycle of activity

Source: Internet
Author: User

Explore life cycle of Activity

1. life cycle analysis in typical situations :onCreate initialization work, loading layout resources and data;onStart ac is starting but cannot interact, backstage;onresume ac is visible, Displayed at the front desk;onPause is stopping and must be executed before the new AC can be executed ;Onstop is about to stop, recycling work; Ondestory imminent destruction, recovery and release of resources;onrestart Restart

2.onStart and onresume,onPause and onStop, what are the real differences.

A :onStart and onresume are callbacks from the perspective of Activity visibility; The Activity is in the foreground at the angle of the callback.

3.in the newActivitybefore starting, the top of the stackActivityneed to firstOnPauseafter the newActivityto start. Not inOnPausein order to do the heavyweight operation because it must beOnPauseafter the execution completes the newActivitytoResume. So: we knowOnPauseand theOnStopare not able to perform time-consuming operations, especiallyOnPause, which also means that we should try toOnStopso that the newActivityshow it as soon as possible and switch to the foreground

4. life cycle analysis in exceptional situations

  1. ) Situation1: A change in resource-related system configuration has resulted inActivitybe killed and re-created
    1. Example: Current Activity is in a vertical state, and if you suddenly rotate the screen, the activity is destroyed and recreated by default due to changes in the system configuration.
    2. in this unexpected situation, the system calls Onsaveinstancestate to save The state of the current Activity. This method only occurs in the event of an abnormal termination, and when the new Activity is created, the system calls onrestoreinstancestate Remove the previously saved data and restore it. in terms of timing, the timing of onrestoreinstancestate calls is after OnStart .
    3. and the Activity , each View has S and R, all through the delegation of thought, the upper level entrusted to the lower layer, for example TextView will save its own text selection and text content.
  2. Insufficient resource memory results in low-priority Activity was killed .

1. A better approach is to keep the background work in Service so that the process has a certain priority.

2.Activity Points by priority: Foreground activity(interacting with user)> visible but not foreground activity (You cannot interact directly after the dialog box has been popped.) > Backstage Activity (suspended).

Android Art--a deep look at the life cycle of activity

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.