Today, I found that I didn't even figure out the bundle class, so I took some time to study it.
According to the official Google documentation (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 relationship:
Java. Lang. Object
Android. OS. Bundle
The bundle class is a final class:
Public final class
Bundle
Extends objectimplements parcelable cloneable
Communication between two activities can be achieved through the bundle class. The practice is:
(1) create a bundle class
Bundle mBundle = new Bundle();
(2) Add data to the bundle class (in the form of key-value, when retrieving data in another activity, key is used to find the corresponding value)
mBundle.putString("Data", "data from TestBundle");
(3) create a new intent object and add this bundle to this intent object
Intent intent = new Intent(); intent.setClass(TestBundle.this, Target.class); intent.putExtras(mBundle);
The complete code is as follows:
Android mainfest. XML is as follows:
<?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> <uses-sdk android:minSdkVersion="7" /></manifest>
The two classes are as follows: intent is initiated 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 intent = new intent (); intent. setclass (testbundle. this, target. class); bundle mbundle = new bundle (); mbundle. putstring ("data", "data from testbundle"); // press the data 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.tar get); bundle = getintent (). getextras (); // get the passed bundle string data = bundle. getstring ("data"); // read data settitle (data );}}
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 to jump </string> <string name = "target"> Target activity </string> </resources>
Result:
Jump Result: