Android Bundle Transfer Data

Source: Internet
Author: User

1. Passing Normal data
                Intent intent=New Intent (mainactivity.  this, twoactivity. class );                Bundle Bundle=new  bundle ();                Bundle.putstring ("name", "Zhang San");                Bundle.putint ("age",);                Bundle.putstring ("gender", "male");                Intent.putextras (bundle);                StartActivity (intent);

Get the data passed

        Bundle bundle=getintent (). Getextras ();        String name=bundle.getstring ("name");        String gender=bundle.getstring ("Gender");         int Age =bundle.getint ("age");    

2. Passing serializable data

1. Create a class implementation serializable

2. Passing Data

                Intent intent=New Intent (mainactivity.  this, twoactivity. class );                Bundle Bundle=new  bundle ();                Person1 P1=new Person1 ("Zhang San", "male",);                Bundle.putserializable ("person", p1);                Intent.putextras (bundle);                StartActivity (intent);

3. Acceptance of data

        Bundle bundle=getintent (). Getextras ();        Person1 P1= (Person1) bundle.getserializable ("person");        String name=p1.getname ();        String Gender=p1.getgender ();         int Age =p1.getage ();

3. Passing parcelable Data

1. Create a class implementation Parcelabel

 Public classPerson3Implementsparcelable {PrivateString name; PrivateString Gender; Private intAge ;  PublicPerson3 (string name, String gender,intAge ) {         This. Name =name;  This. Gender =gender;  This. Age =Age ; }     PublicString GetName () {returnname; }     PublicString Getgender () {returngender; }     Public intGetage () {returnAge ; } @Override PublicString toString () {return"person3{" + "name=" + name + "\" + ", gender=" + gender + "\" + ", age=" + Age + '} '; }     Public Static FinalParcelable.creator<person3> creator=NewParcelable.creator<person3>(){        /*** For external class inverse sequence of the colloquial class array using *@paramSource *@return         */@Override PublicPerson3 Createfromparcel (Parcel source) {return NewPerson3 (source); }        /*** Read data from Parcel *@paramSize *@return         */@Override PublicPerson3[] NewArray (intsize) {            return NewPerson3[size];    }    }; /*** 0 is returned by default *@return     */@Override Public intdescribecontents () {return0; }    /*** Write the values into the parcel *@paramdest *@paramFlags*/@Override Public voidWritetoparcel (Parcel dest,intflags)        {dest.writestring (name);        Dest.writestring (gender);    Dest.writeint (age); }    /*** The read data must be consistent with Writetoparacel (Parcel dest,int flags), otherwise it will be an error *@paramSource*/     PublicPerson3 (Parcel source) {Name=source.readstring (); Gender=source.readstring (); Age=Source.readint (); }}

2. Passing Data

                Intent intent=New Intent (mainactivity.  this, twoactivity. class );                Bundle Bundle=new  bundle ();                Person3 P3=new Person3 ("Zhang San", "male",);                Bundle.putparcelable ("person", p3);                Intent.putextras (bundle);                StartActivity (intent);

3. Acceptance of data

Bundle bundle=getintent (). Getextras ();        Person3 P3= bundle.getparcelable ("person");        String name=p3.getname ();        String Gender=p3.getgender ();         int Age =p3.getage ();

Android Bundle Transfer Data

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.