Bundles Exchange data between activity

Source: Internet
Author: User

Data exchange between activity has a "messenger": Intent, so the main is to put the exchange of data into the Intent can

Intent provides multiple overloaded methods that "carry" additional data :

Put bundle data:

Putextras (Bundle data), put a "carry" packet into the intent

Bundle Getextras (); Take out the packets that intent "carry"

put key value data: (The source layer is actually the bundle used)

PutExtra (String name,xxx value), put data in key-value form, Xxx is any type

Getxxxextra (String name); Remove data of the specified type by key value



Bundle is a simple data carrying packet , bundle method:

Put the basic type:

Putxxx (String key,xxx data); Add various types of data such as Int,long to the bundle

GetXxx (String key); Remove various types of data such as Int,long

put object :

Putseralizable (String key,seralizable data); Put in a serializable object

Getseralizable (String key,seralizable data); Take out a serializable object


In general, the first activity:
* Bundle put data by:
* PUTXXX (String key,xxx value); you want to put a simple data type such as Int,long in a bundle
* Putserializable (String key,serializable data), which can be used to put serializable objects in bundles, mainly for objects
* Intent put bundles by:
* Putextras (Bundle data): Put a packet in the intent that needs to be "carried"

In the activity that was started:
* Intent Fetch data:
* Bundle Getextras (); Remove intent "carry" packet
* Getxxxextras (String key), extract the data of the specified type from the intent by key, and take the data in the bundle in a portable way



Process the registration information with the second activity:

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><tablelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "> <textview android: Id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: text= "Please enter your registration information" android:textsize= "20DP"/> <tablerow android:id= "@+id/tablerow1" Android:la Yout_width= "Wrap_content" android:layout_height= "wrap_content" > <textview android:id= "@+id /textview2 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android oid:text= "Account:" android:textsize= "20DP"/> <edittext android:id= "@+id/name" a            Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:ems= "10" Android:hint= "Please enter your account" AndroidOid:selectallonfocus= "true" > </EditText> </TableRow> <tablerow android:id= "@+id/tab            LeRow2 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "> <textview Android:id= "@+id/textview3" android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:text=" Password: "android:textsize=" 20DP "/> <edittext android:id            = "@+id/password" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"    android:ems= "android:hint=" Please enter the password "android:inputtype=" Textpassword "/> </TableRow> <tablerow android:id= "@+id/tablerow3" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" > <textview android:id= "@+id/textview4" android:layout_width= "Wrap_conte NT "Android:layout_height= "Wrap_content" android:text= "Gender:" android:textsize= "20DP"/> <radiogroup Android:id= "@+id/radiogroup1" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:orientation=" horizontal "> <radiobutton android:id=" @+id/r                Adio0 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:checked= "true" android:text= "male"/> <radiobutton android:id= "@+id                /radio1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "         android:text= "Women"/> </RadioGroup> </TableRow> <button android:id= "@+id/button1" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "register"/>< /tablelayout>

Mainactivity.java

Package Com.hust.bundletest;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import android.widget.radiobutton;/* the registration information with a second activity. * Data exchange between activity is done with intent Messenger.       Intent provides a number of overloaded methods to carry extra data: * 1, bundle data with bundle object, intent carry Bundle Object * Putextras (bundle data): Put a packet that needs "carry" into the intent * Bundle Getextras (); Remove intent "carry" packet * 2, for a simple data type, you can use a portable method of direct storage, take intent to carry the bundle object, source can be found in the first created bundle object * PutExtra S (string key,xxx value); Intent the key value pair (Key-value) in the form of data * Getxxxextras (string key); Extract data of the specified type from intent by key * *//* * Bun Dle is a simple data carrying package that contains several methods: * 1, put in a simple data type * PUTXXX (String key,xxx value), and a simple data type such as Int,long in the bundle, GETXXX ( String key); Take out Int,long simple data type * *, put in serializable object, implement Seriliable interface Object * putserializable (String key,serializable data); You want to put a serializable pair in the bundleLike * getserializable (string key); Take out the Serializable object * General first activity: * Bundle put data by: * PUTXXX (string key,xxx value); think B. A simple data type such as Int,long (String key,serializable data) is placed in the Undle, and a serializable object is placed in the bundle, which is mainly used for placing objects * intent put bundles : * Putextras (Bundle data): Into the intent to put the packet that needs to "carry" * is activated activity: * Intent Fetch DATA: * Bundle Getextras (); Remove in  Tent "carry" Packet * Getxxxextras (String key); Extracts the data of the specified type from the intent by key, and takes the data in the bundle in a portable manner * */public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate)        ;        Setcontentview (R.layout.activity_main);                  Button btn= (button) Findviewbyid (R.id.button1); Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//        Three components obtained EditText name= (EditText) Findviewbyid (r.id.name);        EditText password= (EditText) Findviewbyid (R.id.password); RadioButton male= (RadIobutton) Findviewbyid (r.id.radio0); Determine if String sex= (male.ischecked ()) is selected? "        Male ":" Female "; Encapsulates an object person p=new person (name.gettext (). ToString (), Password.gettext (). ToString (), sex);//Create Bundle Object Bundle Bund Le=new Bundle (); Bundle.putserializable ("person", p);//Create a Intent Object Intent intent=new intent (mainactivity.this,                Resultactivity.class); Intent.putextras (bundle);//start Intent corresponding activitystartactivity (intent);}            }); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid (); if (iD = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

Person.java

Package Com.hust.bundletest;import Java.io.serializable;public class Person implements Serializable {    String name;    String password;    String Sex;public Person (string name, string password, string sex) {super (); this.name = Name;this.password = Password;this . sex = sex;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;}    }

Resultactivity.java
Package Com.hust.bundletest;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;import Android.widget.textview;public class ResultActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_result);//Get Display Components TextView name= (TextView) Findviewbyid (R.ID.TEXT1); TextView password= (TextView) Findviewbyid (R.ID.TEXT2); TextView sex= (TextView) Findviewbyid (R.ID.TEXT3);//Get Intent Object Intent intent=getintent ();//Get sequence data from intent object//person p= (person) Intent.getserializableextra ("person"), equivalent to bundle Bundle=intent.getextras ();//Get Bundle Object Person p= (person Bundle.getserializable ("person"); Gets the serializable object in the//bundle object Name.settext (P.getname ());p Assword.settext (P.getpassword ( )); Sex.settext (P.getsex ());} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater(). Inflate (R.menu.result, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}


Bundles Exchange data between activity

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.