Android Learning Note (33): Activity life cycle

Source: Internet
Author: User

Android is a big app for mobile phones, and some apps have special priorities, such as phones, while the device's memory is limited. Because in some cases the system will kick off the activity so that it is memory. So in the development process, we need to manage the activity life cycle. The image on the right is the time-triggered graph of activity provided in Android Docs.

Four states

In general, at some point, the activity is in one of the following four states:

Active: Started by the user and is running in the foreground.

Paused: A screen that has been started by the user, is running, and is visible, but because of a hint or other coverage section. This is where the user can see the activity but not interact with the activity. For example, have a call, give the answer or reject the choice.

Stopped: User-initiated, running, but hidden due to other activity, unable to render valid information directly to the user, but can communicate through notification.

Dead: Activity does not start or end, for example to free up scarce memory space.

Events of State transitions

When the state changes, Android will provide 1 or more trigger call methods. We do this by rewriting these methods to achieve our processing.

public class MyActivity extends activity{
protected void onCreate(Bundle savedinstancestate) {...}

protected void OnStart() {...}

protected void Onrestart() {...}

protected void Onresume() {...}

protected void OnPause() {...}

protected void OnStop() {...}

protected void OnDestroy() {...}
}

OnCreate () and OnDestroy ()

OnCreate (Bundle), we used to load the XML layout file in OnCreate (). In OnCreate (), the UI is initialized and other initialization is done (only one time, such as binding some listeners). OnCreate () is called in the following three scenarios:

    1. Activity is first started, that is, the first boot from the system power, the bundle parameter is NULL
    2. If Acivity has been run and is later killed off, the parameter is onsaveinstancestate () bundle at the time of invocation.
    3. Activity is already running, but because the state of the different devices requires different resources, such as a vertical screen to a horizontal screen, the acivity will be re-create, and oncreate () will be called

OnDestroy (): Called at the end of the activity life cycle, which is closed, either through the activity active shutdown through finish (), or because the system needs memory and passively shuts down. Note that if RAM is scarce, OnDestroy () may not be called, but Acvitiy will still be closed. Frees the resources obtained in OnCreate () in OnDestroy ().

OnStart (), Onrestart () and OnStop ()

OnStart () When an activity is first called, it will be called when the UI is rendered in the foreground, or when it is hidden and then re-rendered at the foreground, that is, when the activity is visible or re-visible to the user.

Onrestart () is called when an activity is restarted after a stop.

OnStop () is called when the state of the activity changes to the stopped state.

OnPause () and Onresume ()

Onresume () is called when the activity becomes the foreground program, which is when the user interacts with the activity. When the activity is first invoked, or recovered from a stopped state, first the UI is visible-onstart (), and then the UI interacts with-onresume (). When a pop-up dialog box (such as a call that will cause acivity to enter the paused state) is eliminated, the Onresume () is triggered because the UI in the paused state is visible, only overwritten and invalid, so there is no UI visual-onstart () call, Only UI can interact with-onresume () calls. The UI refresh can be done in Onresume ().

Any activity that causes the user not to focus on you, such as other activity startup, OnPause () is called, where we can undo the do in Onresume, stop the background process, release the original resource (e.g. camera), once the OnPause () After being called, when Acivity is killed by the system, it will not receive an event reminder, that is, ondestory () will not be triggered.

Scenario Examples

We look at the status change events of the activity by examining the activity that makes an activity happen.

Scenario 1: If we start another activity (Activity2) in one activity (activity1) through intent, then the triggering process is:

Activity1-onpause ()->activity2-oncreate ()->activity2-onstart ()->activity2-onresume () Activity1-onstop ()

Activity1: Lost the focus of the user, unable to interact with the user, was Activity2 overwritten, called the OnPause (), and then the Activity2 initiated the call process, usually activity2 is not covered part, and completely occupy the screen, This activity1 is hidden and activity1 enters the stopped state, calling Activity1-onstop (). If Acvity2 has a dialog box in a way that is not full screen, i.e. activity1 does not and therefore remains in the paused state, Activity1-onstop () is not triggered.

Activity2: User can interact (get focus) Onresume () for an activity in the boot order of initialization oncreate ()-User visual OnStart ()

Scenario 2: We press the menu return key, exit from Activity2, and revert to activity1

Activity2-onpause (), Activity1-onrestart (), Activity1-onstart (), Activity1-onresume () Activity2-onstop (), Activity2-ondestory ()

Activity1: Enter stopped state in Scene 1, re-restore, enter Onrestart () state, UI Visual OnStart (), get focus user interactive Onresume ()

Activity2: Press the Back button, Acivity will be logged off, according to the figure given by Android, in turn OnPause () lost focus is not interactive->onstop () is not seen->on destory () is closed.

In Mars-android's video teaching, an activity stack becomes a task to explain the situation in which the activity is tuned in turn. But in the updated reference I downloaded, the relevant content has been deleted in the corresponding HTML. However, a description of the task is found in another document. When a user jumps from an activity to an activity, it can be a different app,android system that records the activity's thread navigation history, which is the activity stack, also known as the back stack. The UI at the front of the current screen is the top activity of the stack, and when the activity moves a new activity, the activity-new will join the stack as the topmost element of the stack, and when the user presses the back key, Remove the activity-new from the stack, which is the topmost display of the current UI, and delete the activity-new (enter deaded state). This is a good way to understand how Android handles pressing the back key, which is not changed in a certain order (if we use finish () to remove one of the acitivity and remove it from the stack), The processing method is the advanced post-out of the stack.

Data saving

Android may kick your activiy,activiy because of memory. It will save some state and use this we can save some of the data we need. The way to save the data is Onsaveinstancestate (), note that because the activity is frequently executed, it is important to be efficient and only store our data as bundles. There are two ways to get the data, one is the parameter in the OnCreate (Bundle) carries the data, and the second is the Onrestoreinstancestate (). We can choose the data we need, and this data can even be a callback.

Pop-up dialog box

In our previous studies, the new activity completely covered the original activity, while the other was partially covered, allowing the original activity to enter the paused state instead of the stopped, as shown in the image on the right. program code and Ordinary no difference, the key is Androidmanifest.xml. As follows:

<activity android:name= ". MyActivity "android:label=" @string/myactivity "android:theme=" @android: Style/theme.dialog " />

RELATED links: My Andriod development related articles

Android Learning Note (33): Activity life cycle

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.