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:
[Plain]View Plaincopyprint?
- 10-17 20:54:42.247:i/com.example.servicetest.aactivity (5817): OnCreate () 1166919192 taskid=66
- 10-17 20:54:42.263:i/com.example.servicetest.aactivity (5817): OnStart () 1166919192 taskid=66
- 10-17 20:54:42.263:i/com.example.servicetest.aactivity (5817): Onresume () 1166919192 taskID=66
- 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
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?
The following are comments on several key methods of activity:
void Android.app.Activity.onCreate (Bundle savedinstancestate)
Called when the activity is starting. This is where most initialization should go:calling Setcontentview (int.) to inflate the activity ' s UI, using Findviewbyid To programmatically interact with widgets in the UI, calling Managedquery (Android.net.Uri, string[], String, string[], Str ing) to retrieve cursors for data being displayed, etc.
You can call finish from within this function, in which case OnDestroy () 'll be immediately called without any of the Res T of the activity Lifecycle (OnStart, Onresume, onPause, etc) executing.
void Android.app.Activity.onStart ()
Called after oncreate-or after Onrestart when the activity had been stopped, but was now again being displayed R. It is followed by Onresume.
void Android.app.Activity.onResume ()
Called after Onrestoreinstancestate, Onrestart, or onPause, for your activity-to-start interacting with the user. This was a good place to begin animations, open exclusive-access devices (such as the camera), etc.
Keep in mind that Onresume are not the best indicator that your activity is visible to the user; A system window such as the Keyguard May is in front. Use onwindowfocuschanged to know for certain this your activity is visible to the user (for example, to resume a game).
void Android.app.Activity.onPause ()
Called as part of the activity lifecycle if an activity was going into the background, but had not (yet) been killed. The counterpart to Onresume.
When activity B was launched in front of activity A, the this callback would be being invoked on a. B would not being created until A ' s on Pause returns, so is sure to does anything lengthy here.
This callback was mostly used for saving all persistent state the activity was editing, to present a "edit on place" Model T o the user and making sure nothing is lost if there be not enough resources to start the new activity without first Killi Ng this one. This is also a good place to does things like stop animations and other things that consume a noticeable amount of CPUs in or Der to do the switch to the next activity as fast as possible, or to close resources that is exclusive access such as T He camera.
In situations where the system needs more memory it could kill paused processes to reclaim resources. Because of this, you should be sure, and all of the Your are saved by the time you return from this function. In general Onsaveinstancestate are used to save per-instance state in the activity and this method are used to store global Persistent data (in content providers, files, etc.)
After receiving this call, you'll usually receive a following call to OnStop (after the next activity have been resumed an D displayed), however in some cases there is a direct call back to Onresume without going through the stopped state.
void Android.app.Activity.onStop ()
Called when is no longer visible to the user. You'll next receive either Onrestart, OnDestroy, or nothing, and depending on later user activity.
Note that this method may never is called, in low memory situations where the system does not has enough memory to keep y Our activity's process running after it OnPause method is 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:
[Java]View Plaincopyprint?
- How long we wait for until giving up in 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
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.
Original address: http://blog.csdn.net/zhao_3546/article/details/12843477
Why is Android activity thinning out oncreate, OnStart, Onresume, OnPause, OnStop, Ondesdroy so many ways to get apps to reload? Go