Android_activity life cycle

Source: Internet
Author: User

Android_activity life cycle

Activity is one of the four main components of Android, and each activity has its own life cycle, just as everyone will experience

Born-----Learn to walk, exchange------health------illness------HEAL------Health-------Death

For the understanding of activity, the Android website provides such a picture:

  

Oncreate:activity The first time you create

OnStart: User can see activity, but not yet click action

Onresume: Get focus, can click action

Activity is running:activity normal operation

OnPause: Suspended

OnStop: Stop Running

OnDestroy: Destroyed

Onrestart: Re-run

Process Description: Onpause→onresume activity is not completely covered by another activity, regain focus, restore operational status

Onstop→onrestart→onstar: When the first activity is completely overwritten by the second activity, it returns to the first activity again, from invisible to visible

Execution process when onstop→oncreate process is killed

Activity life cycle: like taking the stairs is the first order of the first order

  

  

Transfer of data between activity:

Now there are Activitya and activityb two activity interfaces

A main interface, B interface must go through a interface jump

When a interface jumps to the B interface: a How to pass data to B, and how the B interface accepts the data passed by the A interface.

The main use of the intent class for delivery, the following code, with detailed annotations

A interface Java code

 Public classMainactivityextendsappcompatactivity {PrivateTextView Textview,textview1;  Public Static FinalString textview_2 = "Textview2"; Private Static FinalString TAG = "Mainactivity"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LOG.D (TAG,"OnCreate:"); FinalBook book =NewBook ("Journey to the Monkey", "Wu Chengen"); Findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent Intent=NewIntent (mainactivity. This, Second_activity.class); //1. Transfer data directly using intent//Intent.putextra (textview_2, "I am a second activity"); //2. Transferring data using bundles                /*Bundle bundle = new bundle ();                Bundle.putstring (textview_2, "I am the second activity created using bundles"); Intent.putextra (bundle,bundle);*/                //3. Passing serializable objectsIntent.putextra ("name", book); //parameter 2: send request int typeStartactivityforresult (intent,999);        }        }); Findviewbyid (R.ID.BUTTON6). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent Intent=NewIntent (mainactivity. This, Dialog.class); Startactivityforresult (Intent,0);    }        }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {TextView=Findviewbyid (R.ID.TEXTVIEW2); Super. Onactivityresult (Requestcode, ResultCode, data); if((Requestcode = = 999) && (ResultCode = = 12)){            if(Data! =NULL) {Textview.settext (Data.getserializableextra ("TextView1"). toString ()); }    }}

B Interface Java code:

 Public classSecond_activityextendsappcompatactivity { Public Static FinalString textview_1 = "Textview1";    TextView TextView; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.second_layout); TextView=Findviewbyid (R.id.textview); //receive data that is passed using intent        if(Getintent ()! =NULL){//NULL pointer exception if NULL is judgedTextview.settext (Getintent (). Getserializableextra ("Name"). toString ()); }        //receive data that is passed using bundles       /*if (getintent () = null) {Bundle bundle = Getintent (). Getbundleextra (Mainactivity.bundle);            if (bundle! = null) {Textview.settext (bundle.getstring (mainactivity.textview_2)); }        }*/Findviewbyid (R.id.button2). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent Intent=NewIntent (); Intent.putextra ("TextView1",NewBook ("Outlaws of the Marsh", "Shi Naian")); //parameter 1: Request a responseSetresult (12, intent);            Finish ();    }        }); }    //overriding the Return key@Override Public voidonbackpressed () {//super.onbackpressed ();//the Finish method is called automatically and needs to be commented outIntent Intent =NewIntent (); Intent.putextra ("TextView1",NewBook ("Outlaws of the Marsh", "Shi Naian")); //parameter 1: request response int TypeSetresult (12, intent);    Finish (); }}

Dialogactivity:

1. Create dialogactivity inherit activity class

2. Modify the theme style of dialogactivity in Manifeste, title

< Activity             Android:name =". Dialog "            android:label=" Please enter the mobile number "            android:theme=" @style/ Theme.AppCompat.Dialog "            >

3. Main activity starts Dialogactiv and sends a data transfer request

Findviewbyid (R.ID.BUTTON6). Setonclicklistener (new  View.onclicklistener () {            @Override              Public void OnClick (view view) {                new Intent (mainactivity.  this, Dialog. class );                Startactivityforresult (Intent,0);            }        );

4.DialogActivity returns data

 Public classDialogextendsappcompatactivity {PrivateEditText EditText; Private Static FinalString TAG = "Dialog"; @Overrideprotected voidonCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.dialog); EditText=Findviewbyid (R.id.edittext); Findviewbyid (R.id.button3). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent Intent=NewIntent (Dialog. This, Mainactivity.class); Intent.putextra ("Name", Edittext.gettext (). toString ()); Dialog. This. Setresult (result_ok,intent); Dialog. This. Finish ();    }        }); }}

5. The main activity receives the data Onactivityresult method

   @Override    protectedvoid onactivityresult (intint  ResultCode, Intent data) {        = Findviewbyid (r.id.textview3);         if ((Requestcode = = 0) && (resultcode = = result_ok)            ){ifnull) {                Textview1.settext (Data.getstringextra ("name"));     }}}

Every life cycle of activity has its meaning, and at what stage it is necessary to execute the corresponding code in its corresponding life cycle method ~

  

  

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