Android first line of code learning Note six---intent passing data to an activity

Source: Internet
Author: User

@1, the next activity passes data:

Intent provides a series of overloads of the Putextra () method that can temporarily present the data that we want to pass in intent, and once another activity is started, simply remove the data from the intent. For example, there is a string in firstactivity to be passed to secondactivity, modify the button click event in firstactivity, code can write:

Button.setonclicklistener (new  View.onclicklistener () {        publicvoid  OnClick (view view) {        String data="Hello secondactivity";        Intent Intent=new Intent (firstactivity.  this, secondactivity. class );        Intent.putextra ("extra_data", data);        StartActivity (intent);    }
});

Here, the intent is used to start the secondactivity, and a string is passed through the Putextra () method. The PutExtra () method receives two parameters, the first parameter is the key, which is used for subsequent values from the Itent, and the second parameter is the data that is actually being passed.

The data passed in the secondactivity is then taken out and printed out with the following code:

 protected           void   OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.secondlayout);        Intent Intent  =getintent (); String data  =intent.getstringextra ( "         Extra_data   "   " secondactivity   " ,data); }

First, through the Getintent () method to get to start the secondactivity intent, and then call the Getstringextra () method to pass in the corresponding key value can get the data passed, Here the string is passed using Getstringextra () to get the data passed, and if it is passed the shaping data, the Getintextra () method is used, and if the Boolean is passed, the Getbooleanextra () method is used, and so on.

@ Up one activity returns data:

Back to previous activity just click on the BAC key to do it, and no one is used to start intent to pass the data. The activity also has a startactivityforresult () method that is also used to initiate activities, but this method expects to return a result to the previous activity when the activity is destroyed. It receives two parameters, the first parameter is intent, and the second parameter is the request code, which is used to determine the source of the data in subsequent callbacks. To modify the Firstactivity button click event, the code is as follows:

Button.setonclicklistener (new  View.onclicklistener () {           publicvoid  OnClick (view view) {           Intent Intent=new Intent (firstactivity.  this, secondactivity. class );           Startactivityforresult (Intent,1);         }
}

Here, using the Startactivityforresult () method to start the secondactivity, the request code as long as it is a unique value can be, here 1, next in the secondactivity button register Click event, And the logic to return the data in the click event, the code is as follows:

 Public classSecondactivity extends Appcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.secondlayout);Button button=(Button) Findviewbyid (R.id.button2); Button.setonclicklistener (NewView.onclicklistener () { Public  voidOnClick (View v) {Intent Intent=NewIntent (); Intent.putextra ("Data_return","Hello firstactivity");                Setresult (result_ok,intent);            Finish (); }        });
}
}

We still build a intent, just to pass the data, it does not specify any intention, followed by the data to be transmitted in the intent, and then called the Setresult () method, which is specifically used to return data to the previous activity, The Setresult () method receives two parameters, the first parameter is used to return the processing result to the previous activity, typically only the RESULT_OK or result_canceled values are used, and the second parameter passes the intent with it. Then call the finish () method to destroy the current activity.

Since we are using the Startactivityforresult () method to start Secondactivity, the Onactivityresult () method of the previous activity is recalled after Secondactivity is destroyed, So we need to rewrite this method in Firstactivity to get the returned data, the code is as follows:

protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Switch(requestcode) { Case 1:                if(resultcode==RESULT_OK) {String Returndata=data.getstringextra ("Data_return"); LOG.D ("firstactivity", Returndata); }                 Break; default:        }    }

The Onactivityresult () method has three parameters, the first parameter is Requescode, which is the request code that we passed in when the activity was started. The second parameter is ResultCode, which is the result of the processing passed in when the data is returned. The third parameter is data. The intent with the returned data. Since it is possible to invoke the Startactivityforresult () method in each activity to initiate many different activities, the data returned by each activity is recalled to the Onactivityresult () method, So the first thing to do is to check the value of the Requestcode to determine the source of the data. Determines whether the data is returned from secondactivity and is determined by the value of ResultCode. Finally, the value is taken from data and printed out, which completes the work of an active return data.

Android first line of code learning Note six---intent passing data to an activity

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.