Activity Learning (1) -- Status Change

Source: Internet
Author: User

(From: http://blog.csdn.net/flowingflying/article/details/6415323)

Many Android applications use mobile phones. Some applications have special priority levels, such as phones, and the memory of devices is limited. In some cases, the system will kick the activity to determine whether the memory is used. Therefore, in the development process, we need to manage the lifecycle of the activity. The right figure shows the activity time trigger chart provided by Android docs.

Four statuses

Generally, an activity is in one of the following four States at a given time point:

Active: Started by the user and running on the foreground.

Paused: The screen is started by the user and is running and visible, but is covered by prompts or other elements. You can see the activity but cannot interact with the activity. For example, if you have an incoming call, you can choose to answer or reject the call.

Stopped: Started and running by the user, but hidden due to other activities, the user cannot directly present valid information, but can communicate through notification.

Dead: Activity is not started or terminated, for example, to release the memory space in short supply.

Status transition events

When the status changes, Android provides one or more call methods. We rewrite these methods to implement 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 XML layout files in oncreate. In oncreate (), initialize the UI and complete other initialization (only one time is required, such as binding some listeners ). Oncreate () is called in the following three cases:

  1. Activity is started for the first time, that is, it is started for the first time after the system is powered on. The bundle parameter is null.
  2. If acivity runs and is later killed, the parameter is the bundle of onsaveinstancestate.
  3. Activity is running, but the status of different devices requires different resources. For example, if the portrait screen is changed to a landscape screen, acivity will re-create and oncreate () will be called.

Ondestroy (): it is called at the end of the activity lifecycle, that is, when the activity is closed. It can be automatically closed by the activity through finish (), or passively closed because the system requires memory. Note that if Ram is insufficient, ondestroy () may not be called, but acvitiy will still be disabled. Release the resources obtained in oncreate () in ondestroy.

Onstart (), onrestart () and onstop ()

When an onstart () activity is called for the first time, it will be called again when the UI is displayed on the front end or hidden in the front end, that is, when the user can see or re-see the activity.

Onrestart () is called when an activity is stopped and restarted.

Onstop () is called when the activity status changes to the stopped status.

Onpause () and onresume ()

Onresume () is called when the activity becomes a foreground program, that is, when the user can interact with the activity. When an activity is called for the first time or recovered from a stopped state, the UI is visible-onstart (), and then the UI can interact-onresume (). When a pop-up dialog box (for example, a call will cause acivity to enter the paused State) is eliminated, onresume () is triggered because the UI is visible in the paused state, it is not valid because it is overwritten. Therefore, there is no UI visible-onstart () call. Only the UI can be called by interactive-onresume. You can refresh the UI in onresume.

Any activity that allows the user not to pay attention to you, such as starting other activities, onpause () is called. Here we can stop the background process by undo in onresume, release the previously occupied resources (such as cameras). Once onpause () is called, when acivity is killed by the system, no event notification is received, that is, ondestory () is not triggered ().

Scenario example

We can check the status change events of an activity by checking the status change events of an activity.

Scenario 1: If we start another activity (activity2) through intent in an activity (activity1), the triggering process is:

Activity1-onPause ()-> activity2-onCreate ()-> activity2-onStart ()-> activity2-onResume ()-> activity1-onStop ()

Activity1: it loses the user's focus and cannot interact with the user. It is overwritten by activity2, onpause () is called, and then the start call process of activity2. Usually activity2 is not covered, and occupy the screen completely, thus activity1 is hidden, activity1 enters the stopped state, calls the activity1-onStop (). If acvity2 is in a dialog box, instead of a full screen, that is, activity1 does not and therefore remains in the paused state, the activity1-onStop () is not triggered ().

Activity2: The Starting sequence of an activity is to initialize oncreate ()-> User-visible onstart ()-> User Interaction (Focus) onresume ()

Scenario 2: press the return key of the menu to exit from activity2 and return to activity1.

Activity2-onPause ()-> activity1-onRestart ()-> activity1-onStart ()-> activity1-onResume ()-> activity2-onStop ()-> activity2-onDestory ()

Activity1: in scenario 1, it enters the stopped state, resumes, and enters the onrestart () state. The UI can be viewed as onstart (), and users with focus can interact with onresume ()

Activity2 () disabled.

In the video tutorial of Mars-android, a stack of activity is used to explain how to call activity in sequence. However, in the updated reference I downloaded, the related content has been deleted in the corresponding HTML. However, you can find the task description in another document. When a user jumps from an activity to an activity, it can be a different app. The Android system records the thread navigation history of the activity. This is the activity stack, also known as back stack. The UI at the beginning of the current screen is the activity at the top of the stack. When this activity starts a new activity, this activity-new will be added to this stack as the top element of the stack, when you press the back key, remove the activity-New shown on the top, that is, the current UI, from the stack and delete the activity-New (entering the deaded State ). In this way, we can better understand how Android processes the press of the back key. These activities are in a certain order and cannot be changed. (Of course, if we use finish (), we can delete one of them, removed from the stack). The processing method is the first release of the stack.

Data Storage

Android may kick your Activiy because of the memory, and Activiy will save some status, so we can save some data we need. The method for saving data is onsaveinstancestate (). Note that because the activity is executed frequently, we must ensure the efficiency and only store our data as bundle. There are two ways to obtain data: one is that the parameter in oncreate (bundle) carries the data, and the other is in onrestoreinstancestate (). We can select the desired data, which can even be a callback.

 

Pop-up dialog box

In our previous studies, the new activity completely overwrites the original activity. In another case, the original activity enters the paused State rather than stopped, as shown in.

The program code is no different from the common code. The key is androidmanifest. xml. As follows:

<Activity Android: Name = ". myactivity" Android: Label = "@ string/myactivity"Android: theme = "@ Android: style/theme. Dialog"/>

Activity Learning (1) -- Status Change

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.