The lifecycle of Android development activity is detailed _android

Source: Internet
Author: User

This example describes the lifecycle of the Android activity. Share to everyone for your reference, specific as follows:

The activity class is in the Android.app package, the inheritance system is as follows:

1.java.lang.object

2.android.content.context

3.android.app.applicationcontext

4.android.app.activity

The activity is separate and is used to handle user actions. Almost all activity has to do with the user, so the activity class creates a window through which the developer can place the UI on the window where the activity is created, and when the activity points to the Full-screen window, the Setcontentview It can also be implemented in other ways: as a floating window (through a windowisfloating collection of topics), or embedded in another activity (using Activitygroup). most activity subclasses need to implement the following two interfaces:

the ①oncreate (Bundle) interface is where the activity is initialized . Here you can usually call Setcontentview (int) to set the UI defined in the resource file, using Findviewbyid (int) to get the Windows defined in the UI.

The ②onpause () interface is where the user is prepared to leave the activity, where any modifications should be submitted (usually for ContentProvider to save the data).

In order to be able to use context.startactivity (), all activity classes must define the associated "work" item in the Androidmanifest.xml file.

The activity class is an important part of the Android application lifecycle.

Activity life cycle

The activity in the system is managed by an activity stack. When a new activity starts, it is placed on top of the stack and becomes an activity in motion, the previous activity remains on the stack and is not placed in the foreground until the new activity exits.

A state of four essential differences:

1. At the front of the screen (top of the activity stack), called Active or running state (active or running)

2. If an activity loses focus, it is still visible (a new, non full-screen activity or a transparent activity is placed on top of the stack), called a paused. A paused activity remains active (keeping all state, member information, and the window manager connected), but will be killed when the system memory is extremely low.

3. If an activity is completely covered by another activity, it is called a Stop state (Stopped). It still retains all state and member information, but it is no longer visible, so its windows are hidden, and stopped activity will be killed when system memory needs to be used elsewhere.

4. If an activity is a paused or stopped state, the system can remove the activity from memory, and the Android system deletes it in two ways, either by requiring the activity to end or by killing its process directly. When the activity is displayed again to the user, it must restart and reset the previous state.

The following illustration shows an important state transition of the activity, which indicates the callback interface between the activity and the state transition, and the developer can overload the implementation to execute the relevant code, with a colored oval indicating the state of the activity.

In the above diagram, the activityhas three key loops:

1. The entire life cycle, from OnCreate (Bundle) to the end of OnDestroy (). The activity sets all the "global" states in OnCreate (), releasing all resources in Ondestory (). For example, an activity that has a thread running in the background to download data from the network can create a thread in OnCreate () and stop the thread in Ondestory ().

2. The visible life cycle begins at the end of the OnStart () and the OnStop (). During this time, the activity can be seen on the screen, although it may not be in the foreground and can not interact with the user. Between these two interfaces, you need to keep the UI data and resources that are displayed to the user, such as: You can register a intentreceiver in OnStart to listen for changes in the UI that are causing the changes, and you can log it off in OnStop () when you no longer need to display it. OnStart (), OnStop () can be called multiple times, because the activity can be converted between visible and hidden at any time.

3. The life cycle of the foreground, from Onresume () to the end of OnPause (). During this time, the activity is at the front of all the activity and interacts with the user. Activity can be frequently switched between resumed and paused states, such as when a device is ready to hibernate, when an activity processing result is distributed, when a new intent is distributed. So the code in these interface methods should be very lightweight.

The entire lifecycle of an activity is defined in the following interface methods, and all methods can be overloaded. All activity needs to implement OnCreate (Bundle) initialization settings, most of the activity needs to implement OnPause () to submit changed data, most of the current activity also need to implement Onfreeze () interface, In order to restore the state set in OnCreate (Bundle).

public class activity extends ApplicationContext {
protected void onCreate (Bundle icicle);
protected void OnStart ();
protected void Onrestart ();
protected void Onresume ();
protected void Onfreeze (Bundle outicicle);
protected void OnPause ();
protected void OnStop ();
protected void OnDestroy ();
}

More interested readers of Android content can view this site: "The activity of Android programming skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Development Primer and Advanced tutorials, Android Resource operations tips, Android View overview and Android Control Usage summary

I hope this article will help you with the Android program.

Related Article

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.