Android--activity life cycle specific explanation

Source: Internet
Author: User

I. Re-exploring the activity life cycle
To study the life cycle of the activity, a simple test code such as the following.

 Packagecom.example.testactivity;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.Menu; Public  class mainactivity extends Activity {Private Static FinalString Log_tag ="Mainactivity";@Overrideprotected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);}@Overrideprotected void OnDestroy() {//TODO auto-generated method stub    Super. OnDestroy (); LOG.I (Log_tag,"mainactivity------OnDestroy");}@Overrideprotected void Onrestart() {//TODO auto-generated method stub    Super. Onrestart (); LOG.I (Log_tag,"mainactivity------onrestart");}@Overrideprotected void Onresume() {//TODO auto-generated method stub    Super. Onresume (); LOG.I (Log_tag,"mainactivity------onresume");}@Overrideprotected void OnStart() {//TODO auto-generated method stub    Super. OnStart (); LOG.I (Log_tag,"mainactivity------onStart");}@Overrideprotected void OnStop() {//TODO auto-generated method stub    Super. OnStop (); LOG.I (Log_tag,"mainactivity------onStop");}@Overrideprotected void OnPause() {//TODO auto-generated method stub    Super. OnPause (); LOG.I (Log_tag,"mainactivity------onPause");}@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true;}}

To view the Logcat, output such as the following:
1. Click the Back button to exit after clicking on the app:

2. After clicking into the app, lock the screen directly:

When the screen is unlocked, activity is awakened:

3. Click the app to enter the app, then click the Home button to exit and enter:

4. Click the Home button to enter the direct lock screen

And then wake up

5. Tap the app to exit, then click the Home button and then lock the screen:

Then wake up into

Description
1) The OnPause () method is called when the user leaves the activity (this generally does not mean that the activity is being destroyed). This function is typically used to submit data that needs to be saved.

2) Description of the label
In addition to Label,name,icon, you need to specify one or more intent to determine what the activity can respond to.

Is the filter, action describes the main entry of the application when the activity, specifying that the activity needs to be listed in the system's application list!

Note: Android application requires that all application components (Activity,service,contentprovider,broadcastreceiver) must be displayed for configuration.

Two. Transfer of values between activity

1) bundle (suitable for less data)
Intent Intent = new Intent ();
Intent.setclass (A.class,b.class);
Intent.putextra ("name", "value");
StartActivity (Intent);
And then B.class.
Intent Intent = new Intent ();
Intent.getextras () to get

2) Bundle (use when data is large)
Intent Intent = new Intent (a.class,b.class));
Bundle bundle = new bundle ();
Bundle.putstring ("Stringkey1", "value1");
Bundle.putstring ("Booleankey2", "value2");
Intent.putextra ("key", bundle);
StartActivity (Intent);
And then B.class.
Intent Intent = new Intent ();
Bundle bundle = Intent.getbundleextra ("key");
Bundle.getboolean ("Booleankey2");
Bundle.getstring ("Stringkey1"), to obtain.
3) Parsing Startactivityforresult
Applies to a jump to B, and then back to a, at the same time to retain the previous user input. Since page a jumps to page B, activity on the page where A is located has been destroy. So, assuming that you want to return a and display the data again, you must wake up the activity on the page where A is, and call a method at the same time to fetch and display the corresponding data.
So generally use the following steps to achieve
A. Jump from A to B using Startactivityforresult (Intent Intent, int requestcode) method;
B. The activity rewrite Onactivityresult method on page A,
Onactivityresult (int requestcode, int resultcode, intent data);
The 1 is provided to the Onactivityresult to confirm which activity the data was returned from, and the number of the reference is corresponding to the requestcode of the Startactivityforresult method when jumping from a to B.
Parameter 2 is used when the new activity returns data to the previous activity before closing.
The parameter 3 is a intent with data returned from activity B.

a page Activity implementation Onactivityresult (int requestcode, int resultcode, intent data) {String re Sult = Data.getextras  () .getString  ()  }b activity implementation: Intent Intent = new Intent ()  intent.putextra  (, )  B (result_ok,intent)  B.this   Not to be continued ... ---------------------------------------good, and then today to further analyze the life cycle of the activity. 

Draw a picture of your activity's life cycle and feel very bad.

As you can see, the entire life cycle of activity has four states, and the descriptive narrative of these four states usually uses 7 methods to complete.
Each of the four states of activity is:

 a. Running when activity is performed on the screen foreground (at the top of the activity stack), it acquires the focus that corresponds to the user action, which belongs to the running state, where only one activity can be in the running (active) state at the same time. B.paused at this point, the activity has lost focus, but still visible to the user, such as: there is another transparent activity on the activity or toast,alertdialog, such as a pop-up form, the activity is in a paused state. The paused activity is still alive, (it retains all of the state and member information and maintains a connection to the form manager), but when this occurssystemWhen memory is not enough, it will be killed by the system. c.stopped specifies that when activity is completely obscured by an activity, it will be in an outage state, but it retains all of its state and member information. Only the activity specified at this time is not visible to the user, but when this occurssystemWhen memory is not enough, it will be killed by the system. D.dead activity has not been started, or has been manually aborted, or has beensystemRecovery. Causes activity to be inactive. The manual abort is capable of invoking the finish method in the program;systemRecovery, it is possible that there is insufficient memory. WhensystemWhen memory is low, the Dalvak virtual machine's memory recycling rules are as follows: First: Processes that are not related to other activity,service,intent,receiver are first recycled (the first to reclaim independent Activity). Therefore, some time-consuming operations are best written as service.Second: Activity that is not visible (stopped).     Third:service process (taken after the first two steps, when memory is low). Fourth: currently executing (Running) Activity.

7 ways to accompany the activity life cycle:

When activity enters from a state and there is a state, the system will voluntarily invoke the following appropriate method to notify the user of such a change.a. ActivityThe system invokes the first time it is instantiatedonCreate ()Method (the entire life cycle is called only once) to initialize the settings: provide the desired layout file for the activity, and static settings such as the button binding listener.b. ActivityVisible, but not yet focusable, that is, when the user cannot interact with the call:OnStart ()C. ActivityCalled when the system starts again after it has been stoppedOnrestart ();d. System calls when activity obtains user focus to interactOnresume (). e. When the system initiates another activity, it is used by the system to save persistent data in the activity that currently has the user focus, stop the animation, and so on, before the new activity is started. The activity is closed when the system, not the user itself, is reclaiming memory. At this point, the user expects that when he returns to the activity again, it remains the same as when it was last left, and then callsonsaveinstancestate (), this method is used to save the state before the activity is killed, it is triggered before onpause, and when the system destroys the activity (perhaps the user does not want to destroy it) in order to save memory overhead, it is necessary to override this method when the activity is instantiated again.onCreate (Bundle saveinstancestate)The temporary state data that has been saved is passed in. It is important to note thatonsaveinstancestate ()method is not often called, its default trigger condition is to press the home button, press the power button, the screen switch three kinds of situations. So we can rewriteonsaveinstancestate ()To record the temporary data of the activity. While recording persistent data, you should use theOnPause ()to store. F. System invoke when activity is completely invisible to an activity overlayonStop ()G. System invoke when activity is killedOnDestroy (), used to releaseonCreate ()method to create a resource, such as an end thread. It is important to note that the oncreate corresponds to theOnDestroy ()method, which is called only once during the entire life cycle of the activity.

At this point, the activity of the comb has almost the same, and finally need to note that the setting activity of the android:configchanges= "" setting.

Android--activity life cycle specific explanation

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.