Android Bundle Class

Source: Internet
Author: User

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
    1. 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
    1. Mbundle.putstring ("Data", "data from Testbundle");


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

[CPP]View Plaincopy
    1. Intent Intent = new Intent ();
    2. Intent.setclass (Testbundle. This, Target.    class);
    3. Intent.putextras (Mbundle);

The complete code is as follows:

Android Mainfest.xml are as follows:

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="Http://schemas.android.com/apk/res/android"
  3. package="Com.tencent.test"
  4. android:versioncode="1"
  5. Android:versionname="1.0" >
  6. <application android:icon="@drawable/icon" android:label="@string/app_name" >
  7. <activity android:name=". Testbundle "
  8. Android:label="@string/app_name" >
  9. <intent-filter>
  10. <action android:name="Android.intent.action.MAIN"/>
  11. <category android:name="Android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. <activity android:name=". Target "></activity>
  15. </application>
  16. <USES-SDK android:minsdkversion="7"/>
  17. </manifest>


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

Class 1:testbundle class:

[Java]View Plaincopy
  1. Import android.app.Activity;
  2. Import android.content.Intent;
  3. Import Android.os.Bundle;
  4. Import Android.view.View;
  5. Import Android.view.View.OnClickListener;
  6. Import Android.widget.Button;
  7. Public class Testbundle extends Activity {
  8. private Button button1;
  9. private Onclicklistener cl;
  10. public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13. Button1 = (Button) Findviewbyid (R.id.button1);
  14. CL = New Onclicklistener () {
  15. @Override
  16. public void OnClick (View arg0) {
  17. //TODO auto-generated method stub
  18. Intent Intent = new Intent ();
  19. Intent.setclass (Testbundle. This, Target.    class);
  20. Bundle Mbundle = new Bundle ();
  21. Mbundle.putstring ("Data", "data from Testbundle"); Press-in data
  22. Intent.putextras (Mbundle);
  23. StartActivity (Intent);
  24. }
  25. };
  26. Button1.setonclicklistener (CL);
  27. }
  28. }


Class 2:target

[Java]View Plaincopy
  1. Import android.app.Activity;
  2. Import Android.os.Bundle;
  3. Public class Target extends activity{
  4. public void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.target);
  7. <span style="color: #ff6600;"   >bundle Bundle = Getintent (). Getextras (); </span> //Get bundles sent
  8. String data = bundle.getstring ("data"); Read the data
  9. Settitle (data);
  10. }
  11. }


Layout file:

Main.xml

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:orientation="Vertical"
  4. Android:layout_width="Fill_parent"
  5. android:layout_height="Fill_parent"
  6. >
  7. <textview
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <button
  13. Android:layout_width="Fill_parent"
  14. android:layout_height="Wrap_content"
  15. android:text="@string/button"
  16. Android:id = "@+id/button1"
  17. />
  18. </LinearLayout>

Target.xml

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:orientation="Vertical"
  4. Android:layout_width="Fill_parent"
  5. android:layout_height="Fill_parent"
  6. >
  7. <textview
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="@string/target"
  11. />
  12. </LinearLayout>

String.xml

[Java]View Plaincopy
    1. <?xml version="1.0" encoding="Utf-8"?>
    2. <resources>
    3. <string name="Hello" >hello world, testbundle!</string>
    4. <string name="app_name" > Test bundle Usage </string>
    5. <string name="button" > click to jump </string>
    6. <string name="target" > came to target activity</string>
    7. </resources>


Results:

Jump results:

Android Bundle Class

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.