Full analysis of activity in Android

Source: Internet
Author: User

Life cycle of activity

1.Activity several states

1. Active (Active): When activity is at the top of the stack, it is visible, with the focus of the foreground activity, which can be used to respond to user input.

2, Paused (pause): In general, your activity is visible but does not have focus, such as when the previous activity is full transparent or non-transparent activity, the following activity is located in the Paused state.

3, Stopped (stop): When an activity is completely invisible, it is in this state. The activity is completely invisible at this point, but the activity's state and member information remains in memory.

4, Inactive (destruction): When an activity is killed, it becomes Inactive. Inactive activity will be removed from the activity stack if re-display requires a reboot. The general destruction is controlled by the system Dalvik.

2. Analysis of each function in activity life cycle

1, OnCreate (Bundle saveinstancestate)

This function is called when the activity is first started and is not executed after startup unless the activity in the paused and stopped states is recycled and then restarted due to system memory constraints.

It has a formal parameter saveinstancestate, which mainly save the activity due to lack of system memory and other reasons, the system is implicitly destroyed activity, before being destroyed, the general will call Onsaveinstancestate (), Retains the status information for this activity at this time. In this method, the bundle parameters are passed in, and the system is retained and passed through the OnCreate () method when it is started again.

2, OnStart ()

In general, the method is called after OnCreate () or when the stopped state returns. The user is executed when the form is re-viewed.

3, Onresume ()

Boot process into execution OnCreate ()-->onstart ()-->onresume () or when activity is in stopped state onstopped ()-->onresume ()

OnPause ()-->onresume () when activity is in paused state

4, OnPause ()

The general form is swapped into the background when OnPause () is executed, and permanent data is typically saved in OnPause ().

5, OnStop ()

OnPause ()-->onstop (). The activity page toggles, or the user presses the back key directly to remove the form from the current task, and also executes the onstop ()

6, Onrestart ()

After OnStop () is executed, the form and process are not destroyed by the system, and the user then re-views the form, and the form's Onrestart ()--->onstart () is executed.

7, OnDestroy ()

Execute OnDestroy () when activity is destroyed.

Four modes of loading for activity

1.Activity Stack

Android uses task to manage multiple activity, and when we launch an app, the activity creates a task for it. It then launches the application's entry activity (i.e., activity configured as Main and launcher in Intent-filter).

We can understand the task as the activity stack. Task in the form of a stack to manage activity, according to the stack "advanced out" principle, the first activity is pushed into the bottom of the stack, and then start the activity on the top of the stack. If we press the back key, then the top activity is popped out of the stack.

2. Four modes of loading

You can specify the Android:launchmode property when configuring activity to configure the load mode for activity

1. Standard: Default load mode (always create new instance)

When the target activity is started, a new instance is always created for the activity and placed in the top of the current task's stack.

2, Singletop: Top single case mode (stack top with an instance will no longer create a new instance)

When the target activity is started, if there is already a target activity at the top of the stack, the new target activity instance is not created, but the original is reused.

If no target activity is at the top of the stack, a new target activity instance is created and added to the top of the current task's stack.

3, Singletask: Internal singleton mode (keep only one instance within a task)

If the target activity instance does not exist within the current task, a new target activity instance is created and added to the top of the current task's stack.

When the target activity is started, if there is already a target activity instance at the top of the stack, the new target activity instance is not created, but the original is reused.

When the target activity is started, if there is already a target activity instance in the current task, but not at the top of the stack, all activity above the instance is removed, so that the instance goes to the top of the stack.

4. SingleInstance Global Singleton mode (keep only one instance in all tasks and place this instance with a separate task stack)

If an instance of the target activity does not exist, a completely new task is created, an instance of the activity is created, and added to the top of the new task.

If it already exists, the task where the instance is located will be forwarded to the foreground.

Start and close activity

Start other activity:startactivity (Intent Intent)

Close current Activity:finish ()

Use of Startactivityforresult

A-activity need to perform some data operations in b-activtiy, after jumping to b-activity, b-activity to return the results of the execution of the operation data to a-activtiy, you need to use Startactivityforresult () to start the b-activity.

The three functions used:

Startactivityforresult (Intent Intent, Int requestcode)
Setresut (int resultcode, Intent Intent)
Onactivityresult (int requestcode, int resultcode, Intent Intent)

Steps:

1, in a jump when not using startactivity (intent) This method, but Startactivityforresult (intent, Int requestcode) Form, The Requestcode can be any value greater than or equal to 0.

0);

2, in a rewrite the Onactivityresult method, used to receive the data of the B return, because the ResultCode sent back different and do differential processing.

protected void onactivityresult (intint  resultcode, Intent data) {switch//  ResultCode As a callback token
   Case
-
:    Bundle b//data is a intent    String str=b.getstring ("str1") that is returned in B.   Str is the value of the callback break    ; default :      Break ;    }}

3. Use the Setresult method in B, and then call the Finish method.

Intent Intent=new  Intent ();  Intent.putextra ("str1", Str_bookname);   Setresult (data);   //

Use bundles to transfer values between activity

The activity uses bundle packets to carry data, and the bundle object has the following methods:

    • Save

Putxxx (String key,xxx data): Putting various types of data, such as int, String, into bundles

Putserializable (String key,serializable data): Putting serializable objects into bundles

    • Take

GetXxx (String key): Take out various types of data such as int, string, etc.

Getserializable (String key): Take out the Serializable object

Of course we can also directly call the intent object's Putextra (String key,xxx data) method to save the data, but it is essentially creating or using the bundle object to pass the value.

Full analysis of activity in Android

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.