Getting Started with Android: Activity lifecycle

Source: Internet
Author: User


introduction of activity lifecycle


We learn the lifecycle of the servlet when we are learning the Java Web, so we have a certain understanding of the concept of life cycle, simply that the process of something from birth to death.

The activity also has a declaration cycle, which is created to the last to be destroyed, and events may be affected by a number of unexpected incidents, such as when a call is made when a function is used, the activity needs to be set in the lifecycle functions;

The following figure is the most classic diagram in the activity lifecycle:




7 Life cycle methods:


(1) onCreate (Bundle Bundle); Create activity

(2) OnStart (), calling when an activity is opened

(3) Onresume (); Called when the activity is displayed

(4) OnPause (); Called when an activity is overwritten but not completely overwritten

(5) OnStop (); Called when an activity is overwritten

(6) Onrestart (), called when an activity is overwritten and then displayed again

(7) OnDestroy (), last destroyed, call


These methods can be summed up with several rules:


(1) onCreate ()-->onstart ()-->onresume () to begin the process of displaying activity

(2) Onresume ()-->onpause () The activity is not at the front, but the visible process

(3) Onresume ()-->onpause ()-->onstop () is a process covered by the activity

(4) OnPause ()-->onresume () is the process from which the activity is visible but not to the front and back to the front.

(5) OnStop ()-->onrestart ()-->onstart ()-->onresume () for activity from not visible in the front of the process


second, non-lifecycle but important method


When an application encounters an unexpected condition, such as low memory, the user presses the home key and so on to need to save some temporary data, for example you fills in the EditText to write half, you pressed the wrong key, presses into the home key, if the default situation, will lose all input information, but this will have the loss to the user, So we need the following two methods:

1.onSaveInstanceState (Bundle Bundle); Save data in case of accident

Save the data call when the accident occurs

From run state to pause state or stop state call

Called when the screen rotates

2.onRestoreInstanceState (Bundle Bundle); Recover data


Like what:



We want to keep the information that EditText entered, so the code is:

Package com.xiazdong.activity.life;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.EditText;

public class Mainactivity extends activity {
	private edittext edittext;
	@Override public
    void OnCreate (Bundle savedinstancestate) {
        super.oncreate (savedinstancestate);
        Setcontentview (r.layout.main);
        EditText = (edittext) This.findviewbyid (R.id.edittext);
    }

	@Override
	protected void onrestoreinstancestate (Bundle savedinstancestate) {
		String name = Savedinstancestate.getstring ("name");	Restore the EditText data
		edittext.settext (name);
		Super.onrestoreinstancestate (savedinstancestate);
	}

	@Override
	protected void onsaveinstancestate (Bundle outstate) {	//Save EditText Data
		outstate.putstring ( "Name", Edittext.gettext () + "");
		Super.onsaveinstancestate (outstate);
	}
    

Third, some other settings


<activity android:theme= "@android: Style/theme.dialog"/> can set activity into a window mode;





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.