Android Life cycle & Activity life cycle

Source: Internet
Author: User

Learn how the process priorities of Android systems change

1.Android systems are typically run on resource-constrained hardware platforms, so resource management is important for Android systems

2. In order to ensure that the high priority program runs properly, the low priority program can be terminated without any warning and the system resources used are recycled, so the Android program does not fully control its life cycle, but is scheduled and controlled by the Android system.

The 3.Android system does not actively terminate the application as much as possible, even if the end-of-life program is saved in memory to start again quickly

4. However, when memory is tight, the system clears the process based on the priority of the process and reclaims the system resources

Process priority in Android:

Foreground process--visible process--service process--------NULL process

(High-priority) (Medium priority) (Low priority)

Foreground process: Is the most important process in the Android system, is the process that the user is interacting with

Visible process: Refers to some program interface can be seen by the user, but not in the foreground and user interaction, do not respond to the interface time process

Service process: A process that contains a service that has been started is a service process. The service process does not have a user interface, does not interact directly with the user, but is able to run in the background for a long time, providing important features that the user cares about (such as playing MP3 files or downloading data from the network)

Background process: If a process does not contain any services that have already been started, and there is no user visible activity, then this process is a background process (such as a process with only the activity component, when the user launches another application that causes the activity of the process to be completely obscured, Then this process becomes the background process)

Empty process: An empty process is a process that does not contain any active components (such as a process with only the activity component, which becomes an empty process when the user closes the activity)

Note:

The priority of a process depends on the highest-priority part of all components (for example, a process that contains both partially visible activity and a service that has already been started, which is a visible process, not a service process).

The priority of the process will vary depending on the dependencies with other processes (for example, process A's service is called by Process B, if the pre-process A is a service process, and process B is the foreground process, then process A also has the priority of the foreground process)

Android components

1.Android applications consist of components, which are basic functional modules that can be called

2.Android system uses components to implement module calls within or between programs to resolve code reuse issues

3. When programming, declare shareable components in Androidmanifest.xml, and other applications can call these shared components directly after declaration

4.Android allows shared components to be instantiated directly by the Android system, guaranteeing the ability to invoke shared components that the process does not start (the Android system does not use the common Application portal (main function))

Four components of Android

Activity, Service, Broadcastreceiver, and ContentProvider, respectively:

Activity:

Is the rendering layer of the Android program, which displays the visual user interface and receives interface events (similar to the concept of a form) generated by user interaction. An activity is an interface that is the façade of the entire program and is responsible for interacting with the user (acting as a program UI) example: Comparing activity to a website, There are a lot of Web pages (ACtivity), the Web page is responsible for displaying content and receiving user data

Service:

Typically used for applications that do not have a user interface, but that need to run in the background for long periods of time, with no graphical interface (the Sand monk in the West, dirty live dirty He did)

Broadcastreceiver:

is a component that receives and responds to broadcast messages, broadcasts a receiver, listens to everything that happens to the phone (monitoring system behavior (no power, broken network ...)), and responds to the various situations that appear in the mobile phone

ContentProvider:

is a standard data sharing mechanism provided by the Android system, where applications can access private data from other applications via ContentProvider. The method of exposing data outward, responsible for providing data externally and allowing the required applications to access the data (library)

Other important components:

Intent and Intentfilter:

is a communication carrier between different components within the Android application, and communication between the Activity, Service, and broadcastreceiver three components is intent as a carrier

Common Properties for UI components:

Android:id specifying Identity, using: @+id/< identifier > Example: Android:id = "@+id/show"

Get component in Java: Findviewbyid ("id"); Example: Findviewbyid (r.id.show);

ANDROID:LAYOUT_WIDTH Specifies the interface component width.

ANDROID:LAYOUT_HEIGHT Specifies the interface component height.

Property value: Wrap_content package content match_parent has the same width (height) as the parent container

Activity:

Simply put: Activity is an interactive interface that fills the window or hovers over other windows. In an application that is typically composed of multiple activity, a master activity is specified in the Manifest.xml, as set

<activity

Android:label= "@string/app_name"

Android:name= ". Mainactivity ">

<intent-filter >

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

    • When the program first runs, the user will see the activity, which can be manipulated by initiating other activity.
    • This current activity will stop when other activity is started, and the new activity will be pushed into the stack and the user focus will be taken while the activity is being manipulated.
    • We all know that the stack is advanced after the principle, then when the user presses the back key, the current activity is destroyed, the previous activity resumed.

Activity life cycle

The activity life cycle is the process of activity from initiation to destruction, and in this process there are 4 states:

Active state, paused state, stop state, inactive state

1. Active Status: Fully visible, positive interaction (current activity in foreground, user visible, can get focus)

2. Paused state: Partially visible, non-interactive (other activity is in the foreground, the activity remains visible, but cannot get the focus)

3. Stop state: Invisible, non-interactive (the activity is not visible, loses focus)

4. Destroy state: The activity ends, or the Dalvik process where the activity is located is ended

An activity is usually a separate screen, and a program typically has multiple activity,activity lifecycles that refer to the activity from the start to the destruction process. The 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 running activity exits.

Stack is advanced after the principle, then when the user presses the back key, the current activity is destroyed, the previous activity resumed.

7 methods and 3 phases of the activity life cycle:

7 Methods (Event callback function):

void OnCreate (Bundle savedlnstancestate)

A callback is called when the activity is created, and the method is only invoked once

void OnStart ()

Callback when activity is started

void Onrestart ()

Callback when activity is restarted

void Onresume ()

Callback when the activity is resumed, the OnStart () method will definitely callback the Onresume () method

void OnPause ()

Callback when activity is paused

void OnStop ()

Callback when activity is stopped

void OnDestroy ()

The callback is called when the activity is destroyed, and the method is only recalled once

Overwrite OnPause () Situation: The user is playing a game, there is a phone call in, you need to pause the current (game), and save the game's status, this time can be overridden by the OnPause () method to achieve.

Next, when the user switches to the game's status again, Onresume () is called back, so you can restore the game state by overriding the Onresume () method.

As the activity's own state changes, the Android system invokes different time callback functions,

The developer adds code to the event callback function to do the right thing when the activity state changes

    • The activity goes through the following 3 stages:
    • Start activity: The method of performing 3 life cycles sequentially in this phase, respectively, is the OnCreate, OnStart, and Onresume methods.
    • Activity regain focus: If activity is re-focused, it will perform 3 lifecycles in turn, Onrestart, OnStart, and Onresume
    • Close activity: When activity is closed, the system executes 3 life cycle methods, namely OnPause, OnStop, and Ondestory.

Activity life cycle can be divided into full life cycle, visual life cycle and activity life cycle

Full life cycle:

The entire process from activity creation to destruction begins at OnCreate () and ends with OnDestroy (). In general, the global resources and states that the activity can use are initialized in OnCreate (),

and release these resources in OnDestroy (). For example, using a background thread in activity, you need to create a thread in OnCreate (), stop and destroy the thread in OnDestroy ().

In some extreme cases, the Android system will not call OnDestroy () and terminate the process directly.

Visual life cycle:

Activity on the interface from visible to invisible process, starting at OnStart (), ending in OnStop (). Onstrat () is typically used to initialize or launch resources related to the update interface.

OnStop () is typically used to pause or stop all threads, timers, or service associated with the update interface, since the activity is no longer visible to the user after calling OnStop (), and updating the user interface is meaningless.

The Onrestart () function is called before OnStart () to perform some specific processing in the process where the activity is not visible and becomes visible. Because the activity continues to become invisible from the visible,

So OnStart () and OnStop () are called multiple times. In addition, OnStart () and OnStop () are often used to register and unregister broadcastreceiver.

Activity life Cycle :

At this point the activity is at the top level of the screen and can interact with the user, starting at Onresume () and ending with OnPause ().

Because Onresume () and OnPause () are often called during the state transition of the activity, simple and efficient code should be used in both functions.

Android Life cycle & 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.