//mainactivity PackageCn.eoe.helloeoe;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid. r.string;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsactionbaractivity {PrivateButton btnStartAty1; PrivateTextView tvout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); BtnStartAty1=(Button) Findviewbyid (r.id.btnstartaty1); Tvout=(TextView) Findviewbyid (r.id.tvout); Btnstartaty1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent I=NewIntent (mainactivity. This, Aty1.class); I.putextra ("Text", "Get from main");//startactivity (i);Startactivityforresult (i,0); } }); System.out.println ("oncreate!!"); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {String result= Data.getstringextra ("Result"); Tvout.settext (result); Super. Onactivityresult (Requestcode, ResultCode, data); } @Overrideprotected voidOnStart () {System.out.println ("OnStart"); Super. OnStart (); } @Overrideprotected voidOnresume () {System.out.println ("Onresume"); Super. Onresume (); } @Overrideprotected voidOnPause () {System.out.println ("OnPause"); Super. OnPause (); } @Overrideprotected voidOnStop () {System.out.println ("OnStop"); Super. OnStop (); } @Overrideprotected voidOnDestroy () {System.out.println ("OnDestroy"); Super. OnDestroy (); } @Overrideprotected voidOnrestart () {System.out.println ("Onrestart"); Super. Onrestart (); } }
//son activity Public classAty1extendsActivity {PrivateButton btnclose; PrivateTextView tvout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.aty1); Btnclose=(Button) Findviewbyid (r.id.btnclose); Btnclose.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent result=NewIntent (); Result.putextra ("Result", "from Aty1"); Setresult (0, result); Finish (); } }); Tvout=(TextView) Findviewbyid (r.id.tvout); String Frommain= Getintent (). Getstringextra ("text"); Tvout.settext (Frommain); }}
Activity declaration cycles and functions that are called automatically
(1) The first launch will call OnCreate OnStart Onresume
(2) By other activity blocking the current activity calls OnPause
(3) If the activity in OnPause after the system needs more memory, it will kill the activity, and call OnStop;
1, after testing the plugin, you can modify its ID number in the corresponding resource file XML, and use Findviewbyid () to find the plug-in;
2. Monitor button
Btnstartaty1.setonclicklistener (new View.onclicklistener () { @Override public void OnClick (View v) { new Intent (mainactivity. this, Aty1. class ); I.putextra ("text", "Get from Main"); // startactivity (i); Startactivityforresult (i, 0); } );
Setonclicklistener () requires that an instance of Onclicklistener be passed in, and the anonymous function is written directly, and the onclick function is rewritten;
The function uses a intent to load a class of parent acitivity and a child activity, and uses startactivity () to open a new activity;
If you use Startactivityforresult () to open an activity, you can listen to its return value for data interoperability between two activity;
3. Child activity Returns results
Use Setresult () to Exchange data with parent activity, overriding the parent activity
@Override protected void onactivityresult (intint resultcode, Intent data) { = Data.getstringextra ("result"); Tvout.settext (result); Super . Onactivityresult (Requestcode, ResultCode, data); }
Accept the result of setresutl, data is the intent that Setresult returns;
4. Use Finish () to close an activity;
[Android] Activity basics