Android Learning Notes (12)--several ways to use intent to pass data

Source: Internet
Author: User

Several ways to use intent to pass data


Click here for the full code


In addition to returning data from the activity, we often have to pass data to the activity. For this we can use the intent object to pass this data to the target activity.


1. Create a project named Passingdata and add a button to the Activity_main.xml file:

    <button        android:id= "@+id/btn_secondactivity"        android:layout_width= "fill_parent"        android:layout_ height= "Wrap_content"        android:onclick= "OnClick"        android:text= "click to go to Second Activity"/>

2. Add a new Secondactivity.xml file in the Res/layout folder and add the TextView and button:

    <textview        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:text= "Welcome to Second Activity"/>    <button        android:id= "@+id/btn_mainactivity"        android:layout_width= "Fill_parent"        android:layout_height= "wrap_content"        android:onclick= "OnClick"        android:text= "click to Return to main activity "/>

3. Create a new class in the current package named Secondactivity, and add the following code to the Secondactivity.java:

Package Com.example.passingdata;import Android.app.activity;import Android.content.intent;import Android.net.Uri; Import Android.os.bundle;import Android.view.view;import Android.widget.toast;public class SecondActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.secondactivity);//To obtain the data sent through the intent object, first use the Getintent () method to obtain the intent object, Then call the object's Getstringextra () method to get the string value set using the Putextra () method Toast.maketext (this, getintent (). Getstringextra ("str1"), Toast.length_short). Show ();//Ibid., for integer values, call the Getintextra () method (note that if the name does not store a value, the default value is used, 0) Toast.maketext (this, Integer.tostring (Getintent (). Getintextra ("Age1", 0)), Toast.length_short). Show ();//Get Bundle object, use Getextras () Method: For string values, use GetString (), and for integer values, use Getint () bundle bundle = Getintent (). Getextras (); Toast.maketext (This, bundle.getstring ("str2"), Toast.length_short). Show (); Toast.maketext (This, integer.tostring (Bundle.getint ("Age2")), Toast.leNgth_short). Show ();} public void OnClick (View v) {Intent Intent = new Intent () Intent.putextra ("Age3", 45);//callbacks can use the SetData () method (mentioned in the previous article) Intent . SetData (Uri.parse ("Something passed back to main activity");//Set Intent and result code Setresult (RESULT_OK, intent);// Close activity Finish ();}}

4. Add the following code to the Androidmanifest.xml file:

        <activity            android:name= ". Secondactivity "            android:label=" Second Activity ">            <intent-filter>                <action android:name=" Net.zenail.PassingData.SecondActivity "/>                <category android:name=" Android.intent.category.DEFAULT "/ >            </intent-filter>        </activity>

5. Add the following code to the Mainactivity.java file:

public void OnClick (view view) {Intent Intent = new Intent ("net.zenail.PassingData.SecondActivity");//Law One: Use Putextra () Method adds two key/value pairs to the intent object: String and integer type Intent.putextra ("str1", "This is a string"), Intent.putextra ("Age1", 25);// Method Two: Create the Bundle object, add two key/value pairs to it, then use Putextras to add to intent object bundle Extras = new bundle (); Extras.putstring ("str2", "This is Another string "), Extras.putint (" Age2 "), Intent.putextras (extras);//Start activity and set 1 as the request Code Startactivityforresult (Intent, 1);} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Super.onactivityresult (Requestcode, ResultCode, data), if (Requestcode = = 1) {if (ResultCode = = RESULT_OK) {//with GETINTEXTR A () Gets the integer value set with Putextra () Toast.maketext (This,integer.tostring (Data.getintextra ("Age3", 0)), Toast.length_short). Show ();//GetData () Gets the string value set with SetData () Toast.maketext (this, Data.getdata (). toString (), Toast.length_short). Show () ;}}}

6, operation, the effect is as follows:

Click the button:


The following screen appears:





When the message box disappears, click the button and the following screen appears:




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.