Android Intent data transfer

Source: Internet
Author: User

1. Basic data types

New Intent ();   Intent.setclass (activity1. this, activity2. class);   // describe the starting point   and Target New Bundle ();                           // Create a bundle   object Bundle.putstring ("Key", "packaged Data");     // Loading data    Intent.putextras (bundle);                                // Tuck the bundle into the intent    .

2. Two ways of transmitting objects java.io.Serializable and android.os.Parcelable
1. android.os.Parcelable (Android recommended)
1). The passed class object needs to implement the Parcelable interface

 Public classEmployeeImplementsparcelable{ PublicString ID;  PublicString name;  PublicString Dept;  PublicString Idcard;  PublicString Statusint;  PublicString status;  PublicString Mobile;  PublicString sex;  PublicString Sexint;  PublicString address;  PublicString Avatar;  PublicString Education;  PublicString birthday;  PublicString age;  PublicString Dept_name;  PublicString ImageUrl;  PublicString Manager;  PublicString score;  Public Static FinalParcelable.creator<employee> Creator =NewCreator<employee>() {           PublicEmployee Createfromparcel (Parcel source) {Employee Employee=NewEmployee (); Employee.Name=source.readstring (); Employee.age=source.readstring (); Employee.dept_name=source.readstring (); Employee.sex=source.readstring (); Employee.status=source.readstring (); Employee.manager=source.readstring (); Employee.score=source.readstring (); Employee.imageurl=source.readstring (); returnemployee; }           PublicEmployee[] NewArray (intsize) {              return NewEmployee[size];      }      }; @Override Public intdescribecontents () {//TODO auto-generated Method Stub        return0; } @Override Public voidWritetoparcel (Parcel Parcel,intarg1)        {parcel.writestring (name);        Parcel.writestring (age);        Parcel.writestring (Dept_name);        parcel.writestring (Sex);                Parcel.writestring (status);        Parcel.writestring (manager);        Parcel.writestring (score);    Parcel.writestring (IMAGEURL); }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

2) Passing object code

New Intent (Mcontext, anotheractivity.  Class);                     New Bundle ();                    Memployeebundle.putparcelable (Employee_par_key, memployees.get (position));                    Intent.putextras (memployeebundle);                    StartActivity (intent);

3) value in Anotheractivity

Memployee ="Employee Name:" +memployee.name);

2. java.io.Serializable

1) Class object

 Public class Implements Serializable {    /**     * Serialversionuid function is to modify the entity class, can be normal serialization and deserialization, in this don't say, we can Google Baidu under.      */    Private Static Final long serialversionuid = 123456789090L;     Private String name;     Private int Age ;}

2) Pass value and value

Bundle.putserializable (Ser_key, New SER ());  Intent.putextras (bundle);  StartActivity (Intent);  

Android Intent data transfer

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.