Activity data back in Android (using Startactivityforresult)

Source: Internet
Author: User

1, in the first activity (Mainactivity.java) in the method Startactivityforresult (Intent Intent, Int requestcode) Launch the second page (Twoactivity.java);

Bundle bundle = new Bundle (), bundle.putstring ("Strsex", Strsex), bundle.putdouble ("Douheight", douheight); Intent Intent = new Intent (); Intent.setclass (Mainactivity.this, Twoactivity.class); Intent.putextra ("bundle", bundle); Startactivityforresult (Intent, 0);

2. Package the data to be returned in the second activity and return the first page in Method Setresult ();

New Intent (); Intent2.setclass (twoactivity. this, mainactivity. class  New  Bundle (); Bundle2.putstring ("Strresult", strresult); Intent2.putextra (" Bundle2 ", Bundle2); Setresult (0, Intent2); Twoactivity. this. Finish ();

3. In the first activity, the Onactivityresult () method is used to retrieve and process the data returned from the second page and update the page.

@Override protected void onactivityresult (intint  resultcode, Intent data) {    if (0 = =  Requestcode) {        if (0 = = resultcode)            {= Data.getbundleextra ("Bundle2");            = bundle2.getstring ("strresult");            Txtfromact2.settext (STRFROMACT2);        }    }     Super . Onactivityresult (Requestcode, ResultCode, data);}

Requestcode and ResultCode

Equestcode is used to identify the source of the request

ResultCode is used to identify which activity the returned data came from

function of the request code

Using the startActivityForResult(Intent intent, int requestCode) method to open the new activity, we need to startActivityForResult() pass in a request code (the second parameter) for the method. The value of the request code is self-set according to the business need and is used to identify the source of the request.
For example: An activity with two buttons, click on both buttons will open the same activity, regardless of the button to open the new activity, when the new activity is closed, the system will invoke the previous activity onActivityResult(int requestCode, int resultCode, Intent data) method. In the onActivityResult() method you can do this if you need to know that the new activity is opened by that button and you want to do the appropriate business processing:

@Override Public voidonCreate (Bundle savedinstancestate) {.... button1.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {Startactivityforresult (NewIntent (mainactivity. This, Newactivity.class), 1);        }        }); Button2.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {Startactivityforresult (NewIntent (mainactivity. This, Newactivity.class), 2);        }        }); @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {               Switch(requestcode) { Case1:                   //request from button 1 for appropriate business processing                    Case2:                   //request from button 2 for appropriate business processing                }          }}

Effect of the result code
In an activity, you might use the Startactivityforresult () method to open several different activities to handle different businesses, and when these new activity is closed, The system invokes the previous activity's onactivityresult (int requestcode, int resultcode, Intent data) method. In order to know which new activity the returned data comes from, you can do so in the Onactivityresult () method (Resultactivity and newactivity are the new activity to open):

 Public classResultactivityextendsActivity {..... Resultactivity. This. Setresult (1, intent); Resultactivity. This. Finish (); Public classNewactivityextendsActivity {... Newactivity. This. Setresult (2, intent); Newactivity. This. Finish (); Public classMainactivityextendsActivity {//in the activity will open resultactivity and newactivity@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {               Switch(resultcode) { Case1:                   //return data for resultactivity                    Case2:                    //return data for newactivity                }          }} 

Activity data back in Android (using Startactivityforresult)

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.