Android--activity life Cycle detailed

Source: Internet
Author: User

I. Re-exploring the activity life cycle
In order to study the activity life cycle, the simple test code is as follows.

 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, the output is as follows:
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, while retaining the previous user input. Since page a jumps to page B, activity on the page where A is located has been destroy. So, if you want to return a and display the data again, you must wake up the activity on the page where A is located, and call a method to fetch and display the corresponding data.
Therefore, the following steps are generally used 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);
Where parameter 1 is provided to Onactivityresult to confirm which activity the data is returned from, and the parameter corresponds to the Requestcode that uses 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 the activity where B is located.

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

, the activity has four states throughout its life cycle, and the description of these four states is usually done in 7 ways.
The four states of activity are:

 a. Running when activity runs on the front of the screen (at the top of the activity stack), it gets the focus to the corresponding user action, which belongs to the running state, where only one activity is in the running (active) state at the same time. B.paused the activity has lost focus at this point, but remains visible to the user, for example, when there is another transparent activity or toast,alertdialog such as a popup window on top of the activity, the activity is paused. The paused activity is still alive, (it retains all of the status and member information and maintains a connection to the window 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 another activity, it will be in an outage state, but it retains all status 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. Manual abort can call the finish method in the program;systemRecovery, it is possible that there is not enough 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: The currently running (Running) Activity.

7 ways to accompany the activity life cycle:

When activity enters another state from one state, the system automatically calls the appropriate method below to notify the user of this change.a. ActivityThe system invokes the first time it is instantiatedonCreate ()Method (called only once for the entire life cycle) 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 is restarted 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) in order to save memory overhead, it will need to override this method, when the activity is instantiated again byonCreate (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 rewrite it byonsaveinstancestate ()To record the temporary data of the activity. While recording persistent data, you should use theOnPause ()to store. F. System calls when activity is completely invisible to another activityonStop ()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 been almost, and finally need to note that the setting activity of the android:configchanges= "" setting.

Android--activity life Cycle detailed

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.