Why do Android activities need to refine the onCreate, onStart, onResume, onPause, onStop, and onDesdroy methods so that the application can be reloaded ?, Oncreateonstart

Source: Internet
Author: User

Why do Android activities need to refine the onCreate, onStart, onResume, onPause, onStop, and onDesdroy methods so that the application can be reloaded ?, Oncreateonstart
Why do Android activities need to refine the onCreate, onStart, onResume, onPause, onStop, and onDesdroy methods so that the application can be reloaded?

Original link: Workshop.


Recently, I was studying the Activity startup process. Lao Luo's blog was reading and learning about other materials. He also talked to the Android4.3 source code,

In the process of coding, I suddenly thought of the following problem:

Why do Android activities need to refine the onCreate, onStart, onResume, onPause, onStop, and onDesdroy methods so that the application can be reloaded?

Too many content translated and reproduced Based on Android development specifications on the Internet is not the answer I want, so I can analyze it myself.


The following is a typical log of switching between activities, switching from AActivity to BActivity:

10-17 20:54:42.247: I/com.example.servicetest.AActivity(5817): onCreate() 1166919192 taskID=6610-17 20:54:42.263: I/com.example.servicetest.AActivity(5817): onStart() 1166919192 taskID=6610-17 20:54:42.263: I/com.example.servicetest.AActivity(5817): onResume() 1166919192 taskID=6610-17 20:54:46.997: I/com.example.servicetest.AActivity(5817): onPause() 1166919192 taskID=6610-17 20:54:47.021: I/com.example.servicetest.BActivity(5817): onCreate() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onStart() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onResume() 1166971824 taskID=6610-17 20:54:47.099: I/com.example.servicetest.AActivity(5817): onStop() 1166919192 taskID=66

The following logs are generated when you switch from AActivity to BActivity:

10-17 20:54:46. 997: I/com. example. servicetest.AActivity (5817): onPause ()1166919192 taskID = 66
10-17 20:54:47. 021: I/com. example. servicetest. BActivity (5817): onCreate () 1166971824 taskID = 66
10-17 20:54:47. 028: I/com. example. servicetest. BActivity (5817): onStart () 1166971824 taskID = 66
10-17 20:54:47. 028: I/com. example. servicetest. BActivity (5817): onResume () 1166971824 taskID = 66
10-17 20:54:47. 099: I/com. example. servicetest.AActivity (5817): onStop ()1166919192 taskID = 66


The onPause () of AActivity is called first, then the initialization process of BActivity (onCreate () --> onStart () --> onResume (), and then the onStop () of AActivity () called.

I mean, why didn't the onPause () and onStop () of AActivity be called first, and then the initialization process of BActivity (onCreate () --> onStart () --> onResume ())?

Or why is it not the first BActivity initialization process (onCreate () --> onStart () --> onResume (), and then the onPause () and onStop () of AActivity called?


If all initialization is implemented in onCreate (), what is the problem?

First, the Activity is invisible when onCreate () of the Activity is called. If you want to make some animations, since the view does not exist, it is obviously problematic to start the animation in onCreate;

Second, AActivity switches to BActivity and then to AActivity (we assume it is the same instance of AActivity). Because the instance already exists, onCreate will not be called any more. When AActivity switches from the background to the foreground, some initialization may be required, so it cannot be called again;


If all initialization is implemented in onStart (), what is the problem?

First, in the onCreate () annotation, it is clearly recommended that setContentView () and findViewById () be called in onCreate (), but I have tested it, In onStart () it is also normal to call the setContentView () and findViewById () functions;

Secondly, when onStart () is called, the Activity may be visible but not interactive,OnResume ()It is clearly stated that this is not the best indicator that Activity is visible to users. onStart () is called before this, some special initialization-related logic will also be called here.


If all de-Initialization is implemented in onStop (), what is the problem?

1.OnResume ()In the annotations, we recommend that you enable the exclusive device (such as the camera) in onResume () and onResume () corresponds to onPause (), so all the uninitialized operations are placed in onStop ();

2,OnStop ()The onStop () may not be executed if the system cannot retain the process due to insufficient memory.

If the camera application of my old Android mobile phone is not properly closed, the camera will not be able to start normally without restarting the system. It is estimated that it is related to this mechanism; the camera process is forcibly killed, and the initialization operation is not executed normally.


 

Why is the onPause () of AActivity first called during inter-Activity jump, and then the initialization process of BActivity (onCreate () --> onStart () --> onResume ()), then the AActivity onStop () is called?

1.OnResume ()In the annotations, we recommend that you enable an exclusive device (such as a camera) in onResume (). onPause () corresponds to onResume (), the operation to disable the camera should also be called in this method; otherwise, consider the following scenarios:

If AActivity opens the camera, we click a button to jump to BActivity. BActivity also wants to open the camera. Assume that AActivity's onPause () is called after BActivity is started,

Then BActivity cannot start the camera normally.

2. In the onPause () annotation, it is also clearly stated that the method is used to stop animation and other CPU-consuming operations. If you do not execute these operations first, it is not logical to start a new application and then execute this operation;

 

The log for switching from AActivity to BActivity is as follows:

10-17 20:54:46. 997: I/com. example. servicetest.AActivity (5817): onPause ()1166919192 taskID = 66
10-17 20:54:47. 021: I/com. example. servicetest. BActivity (5817): onCreate () 1166971824 taskID = 66
10-17 20:54:47. 028: I/com. example. servicetest. BActivity (5817): onStart () 1166971824 taskID = 66
10-17 20:54:47. 028: I/com. example. servicetest. BActivity (5817): onResume () 1166971824 taskID = 66
10-17 20:54:47. 099: I/com. example. servicetest.AActivity (5817): onStop ()1166919192 taskID = 66

It is reasonable to analyze the logic integrity and user experience. When a user triggers an event and switches to a new Activity, the user must want to enter a new view for operations as soon as possible,

As mentioned above, in onResume (), exclusive devices and animations are usually enabled,

To switch from AActivity to BActivity, first perform onPause () operations in AActivity that correspond to onResume (), such as disabling exclusive devices, disabling animations, or other cpu-consuming operations;

To prevent BActivity from using these resources, disabling CPU-consuming operations is also conducive to the smooth running of BActivity.

When onPause () of AActivity is executed at the underlying layer, there is a certain time limit. When ActivityManagerService notifies the application process to suspend the specified Activity, if the corresponding onPause () activityManagerService will force the Activity to be disabled after execution within Ms. The following is the definition of the onPause () Execution timeout constant:

// How long we wait until giving up on the last activity to pause. this // is short because it directly impacts the responsiveness of starting the // next activity. static final int PAUSE_TIMEOUT = 500; // defined in ActivityStack. java

After the resource-consuming part of AActivity is disabled, switch to BActivity and perform BActivity initialization to display the View in BActivity.

When BActivity has been executed and displayed, the user can interact with each other, and then perform the onStop () Operation of AActivity in the background. Even if there are some time-consuming operations, it does not matter, this is executed in the background and does not affect the user experience.




When Will onPause () and onStop () of the android system activity be called? And some problems

The first question: in fact, those operations can be put in the method you mentioned. You can test it by yourself.
Second, the onResume and onPause methods are frequently called. In these two methods, they generally perform very small and time-consuming operations to enhance the user experience. The status you saved now can be ignored by the cpu execution time of the mobile phone, so it is understandable to put it in this method. In addition, the onPause, onStop, and onDestory methods are marked as YES. Since the onPause method is the most likely method to be executed after the Activity is created, when the system needs to recover memory in an emergency, onStop and onDestory may not be executed, so you should save some crucial state attributes in onPause, of course, you should perform some operations selectively, which is not too time-consuming. If it is too time-consuming, other activities cannot be created until the onPause method of the activity is executed and returned.
Third, the onSaveInstanceState () method has a Bundle parameter, which means that the data stored by this method is managed by the system. When your activity is killed, this Bundle parameter will be passed into the Bundle of the onCreate () method when you re-access it. While
Data stored in onPause may be stored on the hard disk.
The third point is my personal understanding.

What is the difference between onStart () and onResume () of Activity in Android?

First, you need to know the four statuses of the Activity:
After a new Active/Runing Activity is started into the stack, it is at the front end of the screen and at the top of the stack. At this time, it is visible and can interact with users.
Paused is the status when the Activity is overwritten by another transparent or Dialog style Activity. At this time, it is still connected to the window manager, and the system continues to maintain its internal status, so it is still visible, but it has lost its focus, so it cannot interact with users.
Stoped is in the Stoped state when the Activity is overwritten by another Activity and the focus is not visible.
The Killed Activity is Killed and recycled by the system or is in the Killed state when it is not started.

Protected void onStart () This method is called after the onCreate () method, or is called when the Activity is switched from the Stop state to the Active state. Generally, onStart () is executed () then run onResume ().
Protected void onResume () is called when the Activity changes from the Pause status to the Active status.

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.