StartActivity and Startactivityforresult are used to open another activity, But the latter adds more features: For example, when Mainactivity uses Startactivityforresult to open otheractivity, after calling Otheractivity's finish () method, The Mainactivity Onactivityresult () method (which needs to be written) is automatically invoked so that further processing can be done. Of course, not only when you open the activity can pass data through intent, when the activity is closed, if you are using Startactivityforresult, you can also pass data through intent. Let's take a look at the example:
1.startActivityForResult (Intent Intent, int requestcode); There are two parameters, the first parameter intent and startactivity () function, the second parameter represents the request code, mainly to indicate the purpose of opening the activity, for example, I have two buttons in an activity, need to open the same activity , when I close the second activity and return to the first activity, I need to deal with different actions, then I need to judge Requestcode.
such as in the example:
Btnjump1.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setclass (Mcontext, Otheractivity1.class); Startactivityforresult (Intent, 21);});
Startactivityforresult (Intent Intent, int request code), the second parameter is the request code, and when the activity is closed, This requestcode will be returned to the first parameter of onactivityresult (int requestcode, ...).
2. Onactivityresult (), when I use Startactivityforresult () to open otheractivity, when I close otheractivity, The previous activity's Onactivityresult () method is called. Here are 3 parameters, the first requestcode is the 1th set of Requestcode, the second parameter is the result code set in Otheractivity, the third parameter is also set in the otheractivity to pass the data.
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {}}
3. ResultCode and Intent data, these two parameters need to be set in the activity that is opened, for example:
Btnclose.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {String name = Mloginbundle.getstring ("name"); String pass = mloginbundle.getstring ("Pass"), Intent data = new Intent (), if (Name.equals ("Xiaoming") && Pass.equals ("123456")) {Data.putextra ("result", "success"); Setresult (3, data);} Else{data.putextra ("Result", "failed"); Setresult (3, data);} Finish ();}});
Setresult (int resultcode, Intent data), this method is used to return the two parameters, of course, the specific data need to add their own.
Paste the following code:
1. Mainactivity.java
Package Com.example.demo_startactivityforresult;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.util.log;import Android.view.Menu;import Android.view.view;import Android.widget.button;public class Mainactivity extends Activity {private static final String TAG = "Mainactivity";p rivate Context mcontext = this; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button BTNJUMP1 = (button) Findviewbyid (r.id.btn_jumpto_other1); Button BTNJUMP2 = (button) Findviewbyid (R.ID.BTN_JUMPTO_OTHER2); Button BtnJump3 = (button) Findviewbyid (R.id.btn_jumpto_other3); Btnjump1.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setclass (Mcontext, Otheractivity1.class); Startactivityforresult (Intent, 21);}); Btnjump2.setonclicklistener (New View.onclicklistener () {@Overridepublic VOID OnClick (View v) {Intent Intent = new Intent (); Intent.setclass (Mcontext, Otheractivity2.class); Startactivityforresult (Intent, 22);}); Btnjump3.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setclass (Mcontext, Otheractivity3.class); Bundle bundle = new Bundle (), bundle.putstring ("name", "Xiaoming"), bundle.putstring ("Pass", "123456"); Intent.putextras (bundle); Startactivityforresult (Intent, 21);});} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {LOG.E (TAG, "Requestcode:" + reque Stcode); LOG.E (TAG, "ResultCode:" + ResultCode); LOG.E (TAG, "Data:" + data); if (Requestcode = = && ResultCode = = 3) {Intent resultintent = data; String result = Data.getstringextra ("result"); LOG.E (TAG, "Result:" + result);}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); REturn true;}}
2. Otheractivity1.java
Package Com.example.demo_startactivityforresult;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.button;public class OtherActivity1 extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.other1_layout); Button btnclose = (button) Findviewbyid (r.id.btn_close); Btnclose.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {setresult (1); Finish ();}});}}
3. Otheractivity2.java
Package Com.example.demo_startactivityforresult;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.button;public class OtherActivity2 extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.other2_layout); Button btnclose = (button) Findviewbyid (r.id.btn_close); Btnclose.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {Setresult (2); Finish ();}});}}
4. Otheractivity3.java
Package Com.example.demo_startactivityforresult;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.button;public class OtherActivity3 extends Activity {private bundle mloginbundle; @Overrideprotected void OnCreate (bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.other3_layout); mloginbundle = Getintent (). GetExtras (); Button btnclose = (button) Findviewbyid (r.id.btn_close); Btnclose.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {String name = mloginbundle.getstring ("name"); String pass = mloginbundle.getstring ("Pass"), Intent data = new Intent (), if (Name.equals ("Xiaoming") && Pass.equals ("123456")) {Data.putextra ("result", "success"); Setresult (3, data);} Else{data.putextra ("Result", "failed"); Setresult (3, data);} Finish ();}});}}
The layout file is just a button that jumps activiyt, so it doesn't stick to the code.
Here are the following:
StartActivity and Startactivityforresult