A thorough understanding of the Android Activity life cycle

Source: Internet
Author: User

Ashamed to say, although has been done for more than a year of Android development, but recently asked about the activity of the life cycle, but feel that they can not be very confident to answer each other's questions, the activity of the life cycle of understanding is not thorough ah.
Now that we have found the problem, we have to solve the problem, not a life cycle, how complex.

First look at the life cycle diagram of the activity on the Android developers Web:

When activity starts: onCreate---> OnStart---> Onresume This process does not need to say much, we all know.
Here is the main talk: When OnPause, when OnStop, when to Ondestory, and when to recover, when Onrestart, when Onresume.
In order to facilitate the description of the current activity called mainactivity, other activity called secondactivity, thirdactivity and so on.
We can see that the reason the activity jumps from the running state to the OnPause state is that the another activity comes into the foreground. That is to say that there is another actvity is started and run, such as mainactivity through startactivity boot secondactivity, then secondactivity in the UI view of the top layer, And mainactivity is no longer the topmost activity, at this time will be onpause, at this time we can still see mainactivity, just Mainactivity has stood behind others.

And when the mainactivity is completely secondactivity blocked, completely invisible, at this time mainactivity will onstop. Let's look at the reason from OnPause to OnStop: The activity is no longer visiable. This means that at this point mainactivity completely invisible, from here we can guess when the home button is pressed, the current activity will be in the OnStop state.
From the life cycle diagram of actvitiy, we can see that activity can stay in both OnPause and onstop in both states, because they can be recovered accordingly.

Then when to stay in OnPause, and when to stay in OnStop it?


The mainactivity is in the OnPause state:


Because the second actvity here is the dialog style:
<activity android:name= ". Secondactivity "Android:theme=" @android: Style/theme.dialog ">
It also verifies that when Mainactivity is no longer at the top, but still visible, mainactivity is in OnPause state, at this point, if you exit (back) second activity, Then mainactivity will be onresume.


The mainactivity is in the OnStop state (note that the third activity here is initiated through startactivity in mainactivity):

Because at this time mainactivity has been completely thrid actvity blocked, completely invisible, so mainactivity in OnStop state, if at this time exit Thrid activity (back key), Then Mainactivity will Onrestart ().


When Ondestory?
When you press the back key in Mainactivity and exit, you will eventually go to ondestory, or you will go to OnDestroy when you call Finish () in your code.


For the left part of the activity life cycle diagram:



When Mainactivity is in OnPause and OnStop state, when the higher priority apps need memory, and the system memory is not enough, then the Android system will kill the process where the mainactivity is located to free up resources.
Of course, after being killed, want to be reborn, that must have to start over: onCreate---> OnStart ()---> onresume ().

Http://www.cnblogs.com/hibraincol/archive/2012/03/06/2382120.html

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.