Android (Java) Learning Notes 146:bundle and intent class use and interaction

Source: Internet
Author: User

Bundle is just a carrier of information the internal content is organized in key-value pairs, and intent isresponsible for the interaction between the activity itself with a bundle. Intent.putextras (Bundle bundle) directly sets the Intent's internal bundle to the Bundle,intent.getextras () in the parameter to obtain the bundle Intent with.

Intent carries the bundle data,bundle is a kind of data package (packing data), using the intent mechanism to carry out different activity communication through the bundle data.

Communication between the two activity can be achieved through the bundle class, which is:

(1) Create a new bundle class

New

(2) The bundle class to add data (Key-value form, the other activity inside the data , it is necessary to use key, tofind the corresponding value)

Mbundle.putstring ("Data", "Data from Testbundle");

(3) Create a new intent object and add the bundle to the intent object

New Intent ();    Intent.setclass (Testbundle. this, Target. class );    Intent.putextras (mbundle);  

The complete code is as follows:

Android Mainfest.xml are as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.tencent.test"Android:versioncode= "1"Android:versionname= "1.0">    <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name">        <ActivityAndroid:name=". Testbundle "Android:label= "@string/app_name">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    <ActivityAndroid:name=". Target "></Activity>    </Application>    <USES-SDKandroid:minsdkversion= "7" /></Manifest> 

The two classes are as follows: Intent from the Testbundle class to the target class.

Class 1:testbundle class:

Importandroid.app.Activity; Importandroid.content.Intent; ImportAndroid.os.Bundle; ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classTestbundleextendsActivity {PrivateButton button1; PrivateOnclicklistener cl;  Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.main); Button1=(Button) Findviewbyid (R.id.button1); CL=NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubIntent Intent =NewIntent (); Intent.setclass (Testbundle. This, Target.class); Bundle Mbundle=NewBundle (); Mbundle.putstring ("Data", "Data from Testbundle");//Press-in dataIntent.putextras (Mbundle);            StartActivity (Intent);        }        };    Button1.setonclicklistener (CL); }}  

Class 2:target

Importandroid.app.Activity; ImportAndroid.os.Bundle;  Public classTargetextendsactivity{ Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.target); <span style= "color: #ff6600;"   >bundle Bundle = Getintent (). Getextras (); </span>//get the bundle coming in.String data = bundle.getstring ("Data");//Read the datasettitle (data); }  }  

Layout file Main.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    ><TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello"    /><ButtonAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/button"Android:id= "@+id/button1"    /> </LinearLayout>

Target.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    ><TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/target"    /></LinearLayout>

String.xml:

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "Hello">Hello World, testbundle!</string>    <stringname= "App_name">Test bundle usage</string>    <stringname= "button">Click to jump</string>    <stringname= "Target">Come to target activity</string></Resources>

Results:

Jump results:

Android (Java) Learning notes 146:bundle and intent classes use and interact

Related Article

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.