Why is Android activity thinning out oncreate, OnStart, Onresume, OnPause, OnStop, Ondesdroy so many ways to get apps to reload?

Source: Internet
Author: User
Tags constant definition switches

Why is Android activity thinning out oncreate, OnStart, Onresume, OnPause, OnStop, Ondesdroy so many ways to get apps to reload?

Original link: http://blog.csdn.net/zhao_3546/article/details/12843477, reprint Please specify, thank you.


Recently in the study activity of the START process, Lao Luo's blog In the Look, also found other information study, also with Android4.3 source code,

In the process of code, I suddenly thought of the following question:

Why is Android activity thinning out oncreate, OnStart, Onresume, OnPause, OnStop, Ondesdroy so many ways to get apps to reload?

On the internet too much according to the Android Development Code translation reproduced content, are not the answer I want, then analyze it by myself.


The following is a typical switch between activity logs, 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 

when triggering a switch from aactivity to bactivity, the log 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


The first aactivity OnPause () is called, then the bactivity's initialization process (OnCreate ()--OnStart ()--onresume ()), then Aactivity () is called.

A bit of meaning, why not first aactivity the OnPause (), OnStop () is called, and then bactivity the initialization process (OnCreate ()--OnStart ()--onresume ())?

Or why not first bactivity the initialization process (OnCreate ()-OnStart ()-Onresume ()), aactivity (), OnPause () are called?


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

First, the activity of the OnCreate () is called, the activity is not visible, if you want to do some animation, since the view does not exist, in the OnCreate to start the animation, there are obvious problems;

Second, aactivity switches to bactivity, then to aactivity (we assume the same instance of Aactivity), because the instance already exists, so OnCreate will not be called again, that aactivity from the background switch to the foreground, It is possible to need some initialization, it can not be called again, there are problems;


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

First, in the OnCreate () note, it is clear that Setcontentview (), Findviewbyid () is to be called in OnCreate (), but I measured it, called onstart () in Setcontentview (), The Findviewbyid () function is also normal;

Second, when OnStart () is called, the activity may be visible, but it is not yet interoperable, andOnresume () 's comments explicitly say that this is not the best indicator that activity is visible to the user, OnStart () Before this is called, there are some special initialization related logic that can be called here and there will be problems.


What is the problem if all the initializations are implemented in OnStop ()?

1, in the comments of Onresume () , it is recommended to open an exclusive device (such as a camera) in Onresume (), corresponding to Onresume () is OnPause (), so all the de-initialization operations are performed in OnStop (), may lead to new problems;

2.onStop () is explicitly written in the note that OnStop () may not be executed in the event that the system is unable to retain the process due to insufficient memory.

My old Android phone camera app if it doesn't shut down properly, the camera can no longer start normally without rebooting the system, it is estimated to be related to this mechanism, and the camera process is forced to kill, which causes the de-initialization operation to not be performed properly.


When the activity jumps, why is the first aactivity OnPause () called, then the bactivity initialization process (OnCreate ()---OnStart ()--onresume ()), And then the Aactivity OnStop () is called?

1, in the comments of Onresume () , it is recommended to open an exclusive device (such as a camera) in Onresume (), and Onresume () corresponds to the OnPause (), the operation to turn off the camera should also be called in this method; Consider the following scenario:

If aactivity opens the camera and we click on a button to jump to Bactivity, Bactivity also wants to open the camera, assuming that Aactivity's OnPause () is called after Bactivity is started.

The bactivity can no longer start the camera normally.

2, OnPause () in the comments, also explicitly said, in this method to stop the animation and other CPU-consuming operations, if not first to perform these operations, the first to start a new application, and then to perform this operation, is indeed illogical;

The log 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

From the perspective of logical integrity and user experience, so the implementation is really reasonable, when the user triggers an event to switch to the new activity, the user must want to enter the new view as soon as possible to operate,

As already stated above, the Onresume () will normally open the exclusive device, turn on the animation, etc.

When it is necessary to switch from aactivity to Bactivity, the OnPause () operation corresponding to Onresume () in aactivity is performed, such as turning off exclusive devices, turning off animations, or other CPU-intensive operations;

To prevent bactivity also need to use these resources, shutdown CPU-intensive operations, but also conducive to the smooth operation of bactivity.

The underlying execution of the aactivity OnPause (), there is a certain time limit, when Activitymanagerservice notifies the application process to suspend the specified activity, if the corresponding OnPause () has not finished in 500ms, Activitymanagerservice will force the activity to close. The following is the corresponding OnPause () execution of the super-constant definition:

    How long we wait for until giving up in the last activity to pause.  This    //are short because it directly impacts the responsiveness of starting the    //next activity.    static final int pause_timeout =;  Defined in the Activitystack.java

Aactivity the portion of the consumed resource is closed, then switches to bactivity to perform the bactivity initialization, showing the view in Bactivity.

When the bactivity has been executed to show, the user can interact, the background to perform aactivity onstop () operation, even if there are some relatively time-consuming operations, there is no relationship, this is performed in the background, it does not affect the user's experience.



Why is Android activity thinning out oncreate, OnStart, Onresume, OnPause, OnStop, Ondesdroy so many ways to get apps to reload?

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.