Even if you find yourself today the bundle class is unclear. So take the time to look into it.
According to Google's Official document (http://developer.android.com/reference/android/os/Bundle.html)
The bundle class is a key-value pair, "a mapping from String values to various parcelable types."
Class inheritance Relationships:
Java.lang.Object
Android.os.Bundle
The bundle class is a final class:
Public final class
Bundle
Extends Objectimplements parcelable cloneable
Communication between the two activity can be achieved through the bundle class, which is:
(1) Create a new bundle class
(2) Add data to the bundle class (the form of key-value, there is a activity inside the data, it is necessary to use key, find the corresponding value)
Mbundle.putstring ("Data", "Data from Testbundle");
(3) Create a new intent object and add the bundle to this intent object
Intent Intent = new Intent (); Intent.setclass (Testbundle.this, target.class); Intent.putextras (Mbundle);
Complete code such as the following:
Android Mainfest.xml such as the following:
<?XML version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" package= "com.tencent.test" android:versioncode= "1" android:versionname= "1.0" > < Application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ". Testbundle " android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> < /intent-filter> </activity><activity android:name= ". Target "></activity> </application>
Two classes such as the following: Intent from the Testbundle class. to the target class.
Class 1:testbundle class:
Import android.app.Activity; Import android.content.Intent; Import Android.os.Bundle; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Testbundle extends Activity { private Button button1;private Onclicklistener cl; public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Button1 = (Button) Findviewbyid (r.id.button1); CL = new Onclicklistener () { @Override public void OnClick (View arg0) {//TODO auto-generated method Stubintent in Tent = new Intent (); Intent.setclass (Testbundle.this, target.class); Bundle Mbundle = new bundle (); Mbundle.putstring ("Data", "Data from Testbundle"), or//press-in Intent.putextras (mbundle); StartActivity (intent);} }; Button1.setonclicklistener (CL);} }
Class 2:target
Import android.app.Activity; Import Android.os.Bundle; public class Target extends activity{public void OnCreate (Bundle savedinstancestate) { super.oncreate ( Savedinstancestate); Setcontentview (r.layout.target); Bundle bundle = Getintent (). Getextras (); Get the bundle String data = bundle.getstring ("data");//read out data settitle; } }
Layout file:
Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " ><textview android:layout_width=" fill_parent " android:layout_height=" Wrap_ Content " android:text=" @string/hello " /><button android: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"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" Fill_ Parent " ><textview android:layout_width=" fill_parent " android:layout_height=" Wrap_content " android:text= "@string/target" /></linearlayout>
String.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "Hello" >hello world, testbundle!</string> <string name= "app_name" > Test bundle Usage </string> <string name= " Button "> Click Jump </string> <string name=" target "> Come to target activity</string></resources >
Results:
Jump results:
Android Bundle category