Android---activity life cycle

Source: Internet
Author: User

It's been a while since you've been using Android for a while, but there's no real understanding of the activity lifecycle. Today, I saw an article, specially quoted to study. Thank you.

the life cycle of Android activity

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 where the developer can put the UI on the activity-created window via the Setcontentview (View) interface, and when the activity points to the full-screen window, It can also be implemented in other ways: as a floating window (via Windowisfloating's Theme collection), or embedded in other activity (using Activitygroup). Most of the 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 window defined in the UI.
    • The OnPause () interface is where the user prepares to leave the activity, where any modifications should be committed (typically used to contentprovider the data).

In order to be able to use context.startactivity (), all activity classes must have the relevant "activity" item defined in the Androidmanifest.xml file.

Activity classes are an important part of the Android app life cycle.

Activity life cycle

Activity in the system is managed by an activity stack. When a new activity is started, it is placed on top of the stack and becomes a running activity, and the previous activity remains in the stack and is no longer placed in the foreground until the new activity exits.

Activity has four essential differences in state:

    1. At the front of the screen (activity stack top), called the 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 state (Paused). A paused activity remains active (keeping all of the 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 retains all state and member information, but it is no longer visible, so its window is hidden and stopped's activity is killed when the 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 requiring the activity to end, or directly killing its process. When the activity is displayed again to the user, it must restart and reset the previous state.

The following figure shows the important state transitions of activity, which indicate the callback interface between activity in state transitions, which developers can override to execute related code, with a colored ellipse indicating the state of the activity.

In, activity has three key loops:

    1. , from OnCreate (Bundle) Start to OnDestroy () end . Activity sets all the "global" states in OnCreate (), releasing all resources in Ondestory (). For example, if an activity has a thread running in the background that is used to download data from the network, the activity can create a thread in OnCreate () and stop the thread in Ondestory ().
    2. visible life cycle , from OnStart () to OnStop () end . During this time, you can see the activity on the screen, although it may not be in the foreground and cannot interact with the user. Between the two interfaces, you need to keep the UI data and resources displayed to the user, such as: You can register a intentreceiver in OnStart to listen for changes in the UI, and when the display is no longer needed, you can unregister it in OnStop (). OnStart (), OnStop () can be called multiple times because the activity can be converted between visible and hidden at any time.
    3. during this time, the activity is at the front of all activity and interacts with the user. Activity can switch between resumed and paused states on a regular basis, for example when a new intent is distributed when the device is ready to hibernate, when an activity processing result is distributed. So the code in these interface methods should belong to very lightweight.

The entire life cycle of an activity is defined in the following interface method, and all methods can be overloaded. All activity needs to implement OnCreate (Bundle) to initialize the settings, most of the activity needs to implement OnPause () to commit the changed data, most of the current activity also need to implement the Onfreeze () interface, To restore the state set in OnCreate (Bundle).

1  Public classActivity extends ApplicationContext {2 3 protected voidonCreate (Bundle icicle);4 5 protected voidOnStart ();6 7 protected voidOnrestart ();8 9 protected voidOnresume ();Ten  One protected voidOnfreeze (Bundle outicicle); A  - protected voidonPause (); -  the protected voidonStop (); -  - protected voidOnDestroy (); -  +}

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