Source: http://blog.csdn.net/randyjiawenjie/article/details/6651437
Today I found that I didn't even know the bundle, so I took the time to study it.
According to Google's official 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 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
[Java]View Plaincopy
- Bundle Mbundle = new Bundle ();
(2) The bundle class to add data (key-value form, the other activity inside the data, it is necessary to use key, to find the corresponding value)
[Java]View Plaincopy
- Mbundle.putstring ("Data", "data from Testbundle");
(3) Create a new intent object and add the bundle to the intent object
[CPP]View Plaincopy
- Intent Intent = new Intent ();
- Intent.setclass (Testbundle. This, Target. class);
- Intent.putextras (Mbundle);
The complete code is as follows:
Android Mainfest.xml are as follows:
[Java]View Plaincopy
- <?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 from the Testbundle class to the target class.
Class 1:testbundle class:
[Java]View Plaincopy
- 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 stub
- Intent Intent = new Intent ();
- Intent.setclass (Testbundle. This, Target. class);
- Bundle Mbundle = new Bundle ();
- Mbundle.putstring ("Data", "data from Testbundle"); Press-in data
- Intent.putextras (Mbundle);
- StartActivity (Intent);
- }
- };
- Button1.setonclicklistener (CL);
- }
- }
Class 2:target
[Java]View Plaincopy
- 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);
- <span style="color: #ff6600;" >bundle Bundle = Getintent (). Getextras (); </span> //Get bundles sent
- String data = bundle.getstring ("data"); Read the data
- Settitle (data);
- }
- }
Layout file:
Main.xml
[Java]View Plaincopy
- <?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
[Java]View Plaincopy
- <?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
[Java]View Plaincopy
- <?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" > came to target activity</string>
- </resources>
Results:
Jump results:
Android Bundle Class