1. Why study the activity cycle
- Activity is one of Android's four main components and is one of the basic content that Android people must understand deeply.
- can improve our ability to solve related problems.
- If you can't understand a problem in depth, you always feel insecure.
2. Activity life cycle Details 2.1 activity life cycle diagram
is the original image of the Android development website, which clearly expresses the location of each method in the activity life cycle and the calling process.
The execution flow of the activity is expressed in good condition, but in the execution of each method, our equipment is in a state, in which the comments are not very detailed and need further study.
2.2 Activity life Cycle core approach
The following table is an explanation of the various approaches to the activity lifecycle of the Android website.
Note: The information described in the table above is not difficult to find:
- When the activity is finished calling the Onresume () method, the activity is visible and can interact with the user.
- The OnStop () method is called when the activity is completely invisible.
- The activity can be killed when the OnPause () method is called, which means that onStop () and OnDestroy () may not be called. Therefore, you should write critical persistent data (such as user edits) to the storage device at Call OnPause ().
- The information that must be retained during the OnPause () call should be selected, because any blocking process in the method interferes with the transition of the next Activity and slows down the user experience.
3. Use a life cycle instance to deepen the impression 3.1 instance code
There are two activity,mainactivity and twoactivity, the code below, followed by a few common operations to see how the lifecycle process works.
Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findviewbyid (R.id.btn_onclick). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {startactivity (NewIntent (mainactivity. This, Twoactivity.class)); } }); LOG.I ("Info", "mainactivity-onCreate ()"); } @Overrideprotected voidOnStart () {Super. OnStart (); LOG.I ("Info", "mainactivity-OnStart ()"); } @Overrideprotected voidOnresume () {Super. Onresume (); LOG.I ("Info", "mainactivity-Onresume ()"); } @Overrideprotected voidOnrestart () {Super. Onrestart (); LOG.I ("Info", "mainactivity-Onrestart ()"); } @Overrideprotected voidOnPause () {Super. OnPause (); LOG.I ("Info", "mainactivity-onPause ()"); } @Overrideprotected voidOnStop () {Super. OnStop (); LOG.I ("Info", "mainactivity-onStop ()"); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); LOG.I ("Info", "mainactivity-OnDestroy ()"); }}
Public classTwoactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_two_layout); LOG.I ("Info", "twoactivity-onCreate ()"); } @Overrideprotected voidOnStart () {Super. OnStart (); LOG.I ("Info", "twoactivity-OnStart ()"); } @Overrideprotected voidOnrestart () {Super. Onrestart (); LOG.I ("Info", "twoactivity-Onrestart ()"); } @Overrideprotected voidOnresume () {Super. Onresume (); LOG.I ("Info", "twoactivity-Onresume ()"); } @Overrideprotected voidOnPause () {Super. OnPause (); LOG.I ("Info", "twoactivity-onPause ()"); } @Overrideprotected voidOnStop () {Super. OnStop (); LOG.I ("Info", "twoactivity-onStop ()"); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); LOG.I ("Info", "twoactivity-OnDestroy ()"); }}3.2 Start mainactivity
Results forecast:
Mainactivity-OnCreate ()
Mainactivity-OnStart ()
Mainactivity-Onresume ()
Real results:
Sure enough.
3.3 Mainactivity to Twoactivity
Results forecast:
Mainactivity–> OnPause ()
Twoactivity–> OnCreate ()
Twoactivity–> OnStart ()
Twoactivity–> Onresume ()
Mainactivity–> OnStop ()
Real results:
Consistent with the forecast sequence.
3.4 Returning from Twoactivity to Mainactivity
Results forecast:
Twoacitvity–> OnPause ()
Mainactivity–> Onrestart ()
Mainactivity–> Onstrat ()
Mainactivity–> Onresume ()
Twoactivity–> OnStop ()
Twoactivity–> OnDestroy ()
Real results:
The order is consistent, proving that the theory is correct.
3.5 Returning to the desktop from Mainactivity
Results forecast:
Mainactivity–> OnPause ()
Mainactivity–> OnStop ()
Mainactivity–> OnDestroy ()
Real results:
"Android" Activity life cycle Research