Activity life cycle in Android _php tutorial

Source: Internet
Author: User
Tags macbook

An explanation of activity life cycle in Android


Hardware and Software Environment

    • Macbook Pro MGX 72
    • Android Studio 1.3.2
    • Genymotion Simulator

Objective

In Android, there are four development components, they are the basis of Android programming, activity is the first and foremost, is the most serious. Today we are going to study the life cycle of the activity.

What is activity?

Activity is one of the most important components of Android development, and almost all interactions are performed in the activity, which is primarily responsible for creating windows where you can call the Setcontentview function to expose the UI controls you need. Let's take a look at an activity life cycle diagram given by the Android API, and if you don't see it at first, you can see the full blog post and look back and deepen your understanding.

Several important callback functions

The following activity methods show the complete life cycle of the activity and, when the state of the activity changes, you can implement some functionality by overriding these methods.

 public class Activity extends ApplicationContext {    protected void onCreate(Bundle savedInstanceState);    protected void onStart();    protected void onRestart();    protected void onResume();    protected void onPause();    protected void onStop();    protected void onDestroy();}

OnCreate

Called when the activity is created for the first time. The creation of the view and initialization of the data are performed here. Sharp-eyed you may have found that the OnCreate method has a bundle savedinstancestate parameter, which will save the state before the activity, which is useful.

OnStart

is called after the OnCreate method

Onresume

is called after the OnStart method. Activity is managed using a stack structure, at which time the activity is at the top of the stack.

Onrestart

This function is called when the activity of stop is restarted, followed by the Onresume.

OnPause

OnPause is called when a new activity enters the top of the stack, that is, the current activity is forced to run in the background.

OnStop

will be called after the OnPause method, when the activity for the user, is already invisible (Invisible). From the stack point of view, there is a new activity stack, or the existing activity is back to the top of the stack.

OnDestroy

This is the last function in the life cycle. In general, the activity has been completed (call the Finish function) or the system in order to reclaim resources to actively destroy the activity, OnDestroy will be called, you can use the Isfinishing () method to distinguish the 2 cases.

In summary, from the beginning of OnCreate to the end of OnDestroy, called the activity of a complete life cycle. Here are 2 more concepts to differentiate.

1. 可见的(Visible,从onStart到onStop,这里的可见,不仅仅是在屏幕上可见)2. 前台运行(Foreground,从onResume到onPause)

In order to understand the order in which these callback functions are called in various cases, we create a new project called Activitylifecycle, and add some print to these callback functions as follows

 @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); EditText = (editText) Findviewbyid (R.id.edittext); LOG.D (TAG, "= = = OnCreate = = = =");} @Overrideprotected void OnStart () {Super.onstart (); LOG.D (TAG, "= = = OnStart = = = =");} @Overrideprotected void Onresume () {super.onresume (); LOG.D (TAG, "= = = Onresume = = = =");} @Overrideprotected void OnPause () {super.onpause (); LOG.D (TAG, "= = = OnPause = = = =");} @Overrideprotected void Onrestart () {Super.onrestart (); LOG.D (TAG, "= = = Onrestart = = = =");} @Overrideprotected void OnStop () {super.onstop (); LOG.D (TAG, "= = = OnStop = = = =");} @Overrideprotected void OnDestroy () {Super.ondestroy (); LOG.D (TAG, "= = = = = = = = OnDestroy");} 

Run the program, you can see from the Logcat

Describes an activity from creation to display that requires the execution of Oncreate-->onstart-->onresume three methods.

When the app runs, if you want to go to open other apps, generally we will go to press the home button to exit, this time what is the callback? Look down.

The execution of the onpause-->onstop, that the activity has not been destroyed, but was covered by other activity, was put in the background to run, this situation with a sudden incident occurred (if there is a call or app is installed) is the same, and when the unexpected event is over, The activity will execute onrestart-->onstart-->onresume, and the entire process can be printed as

When the app is running, if the current activity is the last activity, when we press the back key, the program ends. The Onpause-->onstop-->ondestroy three methods will be executed successively, such as

Next, a slight modification of the project, add the 2nd activity, called Secondactivity, by clicking on a button on the mainactivity to enter the secondactivity, then if you press the Back button, Secondactivity will execute the Onpause-->onstop-->ondestroy, and Mainactivity will execute Onrestart-->onstart-->onresume, as

Onsaveinstancestate and Onrestoreinstancestate

There is a concept here, not mentioned above. After the OnPause, OnStop method call, the activity process is likely to be killed at any time, so should be in the OnPause method of data preservation operations, to avoid data loss. The Onsaveinstancestate method also accomplishes similar functions, and the data is stored in a given bundle. Because Onsaveinstancestate is not an activity lifecycle function, the Android API is more recommended to save data in the OnPause method.

Toggle Screen

Do not do any processing, switch the screen to look at the print, you can find that first destroyed and then re-created the activity

In order to solve the above problems, there are usually several ways

    • Prohibit the vertical and horizontal screen switch (limit screen or portrait screen, compare Rogue Ah, not recommended)
    • Stand-alone layout (create 2 folders Layout-land and Layout-port, the corresponding XML file is unchanged)
    • Prevent activity overloading (add android:configchanges= "Keyboardhidden|orientation" in Androidmanifest.xml, and override the Onconfigurationchanged method)

SOURCE download

http://download.csdn.net/detail/djstavav/9147793

Reference documents

1, http://developer.android.com/intl/zh-cn/reference/android/app/Activity.html
2, http://developer.android.com/guide/topics/manifest/activity-element.html
3, http://developer.android.com/guide/topics/resources/runtime-changes.html

http://www.bkjia.com/PHPjc/1077807.html www.bkjia.com true http://www.bkjia.com/PHPjc/1077807.html techarticle activity lifecycle in Android detailed hardware and software environment Macbook Pro MGX 1.3.2 Android Studio genymotion simulator preface in Android, there are four development components, which are Android. ..

  • 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.