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 ();

Intent intent2 = new Intent (); Intent2.setclass (Twoactivity.this, Mainactivity.class); Bundle Bundle2 = 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.

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {    if (0 = = Requestcode {        if (0 = = resultcode) {            Bundle bundle2 = Data.getbundleextra ("Bundle2");            String strFromAct2 = 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

Use startactivityforresult (Intent Intent, int requestcode) method to open a new activity, we need to for The Startactivityforresult () method passes in a request code (the second parameter). 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 has two buttons, click the two buttons will open the same activity, whether it is the button to open the new activity, when the new activity is closed, the system will call 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 void  onCreate (Bundle savedinstancestate) {... button1.setonclicklistener (new< span> View.onclicklistener () {public void  OnClick (View v) {Startactivityforresult (New in           Tent (Mainactivity.this, Newactivity.class), 1 );        }        }); Button2.setonclicklistener (New  View.onclicklistener () {public void  OnClick (View v) {STARTACTIVITYF Orresult (New Intent (Mainactivity.this, Newactivity.class), 2 );}}); @Override protected void Onactivityresult (int requestcode, int  resultcode, Intent data) {Switch  (requestcode {Case 1 ://Request from Button 1, make corresponding business case 2 ://Request from button 2, make corresponding 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. 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 class Resultactivity extends Activity {       ...       .. ResultActivity.this.setResult (1, intent);       Resultactivity.this. Finish ();} public class Newactivity extends Activity {       ...        NewActivity.this.setResult (2, intent);        Newactivity.this. Finish ();} public class Mainactivity extends activity {//In the activity will open resultactivity and newactivity @Override protected void Onactivi Tyresult (int requestcode, int resultcode, Intent data) {switch(resultcode) {Case 1: return number of//resultactivity According to Case 2://newactivity return Data           

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.