Life cycle of activity

Source: Internet
Author: User

Life cycle of activity

As users navigate through the application pages continuously, the activity switches between different lifecycle states, and when an activity is first called, the Android system invokes a series of methods to start the user interface, and by using these methods, You can create the look and feel of your activity just like you create other components;

If a user initiates another activity or even switches to another application, Android invokes a series of other methods as your activity enters the background, for example: if the user switches to another application, it may be necessary to disconnect the data connection used by the current application ; When these conditions occur, the state of the application may be lost;

If Acticity is in the foreground or at the top of the activity stack, then he is considered to be active (or operational); It has the highest priority in Android and is only terminated by the operating system in extreme cases;

Activity can be paused while the paused state has the second highest priority in the Android system, and when activity is paused, all state and member information is maintained and saved to the window manager;

Activity is stopped or hidden in the background, which usually occurs when one activity is completely overwritten by another activity, and the activity still maintains its state and member information, but in the three states of activity, the priority is the lowest;

When an activity is removed from memory by the Android system, you must restart the application

In summary, you only need to handle changes to the activity state by simply rewriting the relevant methods in the application, and when the activity starts, it calls the OnCreate () method, in which you can create views, initialize variables, and introduce static data But in order not to reduce the speed of this process, it is best to do a few similar processing in the code;

The OnStart method is called after the OnCreate method, and the OnStart method is useful if you need to perform specific operations such as refreshing the current view before the activity is displayed, and the Onresume method is called when the activity is ready to interact with the user. At this point you can do such as display animation effects or related dialog box processing;

The OnPause method is called when the system is about to put activity in the background or the activity part is obscured; you can save any unsaved changes in this method, clean up objects that consume resources, reduce the frame rate, or stop the animation;

The OnStop method is called when activity becomes invisible; This method may not be called at all, so it is best not to rely on it;

The OnDestroy method is the last method to be called before the activity is destroyed or removed from memory, and it is used to clean up processes that could cause a resource leak, such as any background thread;

The Onrestart method is called before the activity has stopped and started again;

Re-discussion on activity life cycle

As you can see, activity is just a screen or user interface for an Android app, a full-screen app or a floating window that interacts with the user, and the Android app consists of different activity that interacts with the user, Interact with other activity too; For example, a simple calculator uses only a single activity; If you refine your calculator application to switch between a simple version and a scientific version, you will use two activity;

Each Android application runs in a separate process, the process is constantly started and terminated in order to run the application, the process may be killed in order to save memory and resources, and the activity is run in the application's UI main thread in turn;

When activity starts, it enters a life cycle (lifecycle); The life cycle is a term that refers to the state that the activity undergoes when the user (or operating system) interacts with the activity Android provides a special callback method for you to respond to changes in the activity declaration cycle;

Activity life cycle of 4 states:

    • When the activity is in the foreground of the application, he is a running activity and only one activity is in operation for a period of time;
    • If the activity loses focus but is still visible (for example, a smaller activity appears on top of the stack), the activity is paused;
    • If the activityb is completely overwritten by another active activity, it will be in a stopped state, and when the activity is stopped, all states will be lost, so that the current state of the user interface needs to be recreated when the activity is restarted;
    • When the activity is paused or stopped, the system can end it in order to reclaim the memory, after which the user can restart the activity;

However, when an application migrates between different states, the android.app.Activity life-cycle method (or callback) is called by the system, and the callback methods are as follows:

    • OnCreate (Bundle savedinstancestate) is called when the activity is first created, when the data should be initialized, the initialization view is created, or the frozen state is saved before the activity is resumed (discussed later) ; OnCreate is usually called after OnStart;
    • OnStart () is called when activity becomes visible, which is best used to write code related to the application user interface, such as handling events that interact with the user; OnStart is usually called onresume, but if the activity becomes hidden, Then the onstop will be called;
    • Onresume () is called when activity becomes foreground and can interact with the user, and is usually called onpause;
    • OnPause () is called when other activity switches to the foreground, OnPause must be implemented very quickly because other activity cannot run until the function returns, and if activity is returned to the foreground, Onresume is called after OnPause If the activity becomes invisible, then the onpause is called OnStop;
    • OnStop is called when activity becomes invisible, whether the new activity is initiated (or the activity that already exists is restored) or the current activity is destroyed, the current activity's OnStop callback is called If the activity returns to the foreground, then OnStop will call Onrestart;

Acenodie on 2015/3/28 15:53 Edit

Let's take a look at the Android lifecycle with specific examples:

1 protected voidonCreate (Bundle savedinstancestate) {2 3 Super. OnCreate (savedinstancestate);4 5 Setcontentview (R.layout.activity_timer;6 7LOG.D ("Life", "onCreate");8 9 }Ten  One  Public voidOnStart () { A  - Super. OnStart (); -  theLOG.D ("Life", "OnStart"); -  - } -  +  Public voidOnPause () { -  + Super. OnPause (); A  atLOG.D ("Life", "OnPause"); -  - } -  -  Public voidOnresume () { -  in Super. Onresume (); -  toLOG.D ("Life", "Onresume"); +  - } the  *  Public voidOnStop () { $ Panax Notoginseng Super. Onresume (); -  theLOG.D ("Life", "OnStop"); +  A } the  +  Public voidOnDestroy () { -  $ Super. OnDestroy (); $  -LOG.D ("Life", "OnDestroy"); -  the } - Wuyi  Public voidOnrestart () { the  - Super. Onrestart (); Wu  -LOG.D ("Life", "Onrestart"); About  $}

What I'm trying to say is, just to find out if OneNote has a better code highlighting plug-in took nearly two hours, just start looking for a new plug-in or I have not used, but have not found, and want to be able to copy the format of the code on Eclipse, Wouldn't it be better to find two methods, one of which is not valid, the other is too complex to try; So I thought of Onenote2016, see if there is a new feature launched, a search will find OneNote launched a novel web editing plugin clliper, simply artifact Ah, Then began to tinker, but the official website can not download, so the group to ask for help, no fruit, then intends to go to the chrome plug-in store, GFW, you know, open Goagen, still invalid, GFW is really increasingly powerful, and think of the Vpn,greenvpn bought in the previous few days, try again, invalid, ask customer service again , not back! , to stick to write a complaint paste, directly by the administrator to delete, ask him why, not back, reported him, and in QQ reported once, finally still humble to stick to ask reason, so far no fruit, began to learn, time is so 1.1 points wasted-_-| | |, I even opened the Sogou input method to find an expression!! Hey, hey! Time is such a waste of Ah, this is in my own write read once again feel, nothing said, hey;

Nonsense not to say, began to analyze;

Open the application, first in turn, OnCreate, OnStart, Onresume, such as:

When I click on the Rotate screen, I follow the section in the black box, followed by: OnPause, OnStop, OnDestroy, OnCreate, OnStart, Onresume, can be found, The last three and above just open the execution of the application of the three method is the same, now can be understood as IS and reloaded again actiity, details are not quite clear, see after;

Confirmed by the online (from

There are also ways to prevent the activity from being destroyed when the screen is turned on, and the principle is unclear:

1) modify androidmanifest.xml

in the activity attribute, add:

Android:configchanges= "Orientation|keyboardhidden" 
    • android: Configchanges activity will handle these changes on its own.
    • Orientation api rank equals or above 13 , you also need to set screensize because screensize changes when the screen rotates
    • Keyboardhidden

As follows:

1 <activity android:theme= "@style/contentoverlay"  2         android:launchmode= "Singletask"  3         android:configchanges= "Keyboardhidden|orientation|screensize"                             4         Android:windowsoftinputmode= "adjustpan|statealwaysvisible" android:name= "com.weibo.net.ShareActivity" >  5 </activity>

When you switch to another application at this point, execute the method in turn: OnPause, onStop;

When I switch back to this application again, execute Onrestart, OnStart, onresume in turn;

Open the app, then click the Back button, execute OnPause, OnStop, OnDestroy

The experiment proves that when the point returns the key, the program has been executed, at this time if it is started again from the recent task, it is equivalent to reopen, and delete the record from the nearest task bar, and no other callback function is called;

At this point I have to record that when I am debugging this timer, I click Start to start the timer, the Settimedisplay () function executes, which is normal, but it is strange that when I click the return key, the function does not stop, but continues to execute, Until I delete the record in the recent taskbar ...

Odd, strange???

Acenodie on 2015/3/28 22:52 Edit

Wait a minute, there is another test not done, that is, after the application appears, press the main screen key back to the main screen, the test is as follows:

Tested in this case, as in the case of switching directly to other applications, execute OnPause and onstop in turn;

Another problem is found here, because, according to the theory, the life cycle of the activity, such as the transition between different states is a lot of situations, and I analyzed above the case is clearly not included in many of the following situations, for example:

From OnPause--?? OnCreate

From OnStop--?? OnCreate

From OnPause--? Onresume

From OnStop--?? OnCreate

These conditions are expected to be tested in future opportunities;

Life cycle of activity

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.