Android Bundle Class

Source: Internet
Author: User

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

    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)

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

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

    1. Intent Intent = new Intent ();
    2. Intent.setclass (Testbundle. this, Target.     class);
    3. Intent.putextras (Mbundle);

The complete code 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 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 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

    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>//Received 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:

Ext.: http://www.cnblogs.com/xiaoruhan/archive/2012/03/02/2377117.html

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.