Passing class objects through Intent

Source: Internet
Author: User

The intent transitive class object in Android provides two ways of passing an object by implementing the Serializable interface , and one is passing an object by implementing the Parcelable interface .

The object required to be passed must implement one of the above 2 interfaces to pass directly through the intent

Methods for passing these 2 kinds of objects in intent:

Bundle.putserializable (key,object);  Implements the Serializable interface object Bundle.putparcelable (Key, object); Objects that implement the Parcelable interface

Package Com.hust.bundletest;import;public class Person implements Serializable {    String name;    String password;    String Sex;public Person (string name, string password, string sex) {super (); = Name;this.password = Password;this . sex = sex;} Public String GetName () {return name;} public void SetName (String 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) { = sex;}    }
Register the form to send intent code:

@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);                  Button btn= (button) Findviewbyid (; Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//        Three components obtained EditText name= (EditText) Findviewbyid (;        EditText password= (EditText) Findviewbyid (;        RadioButton male= (RadioButton) Findviewbyid (; 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);//bundle an object//create a Intent Object Intent intent=new Intent (                Mainactivity.this,resultactivity.class); Intent.putextras (bundle);//start Intent corresponding activitystartactivity (intent);} });
The code of the receiving end:

@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 ());}
The above can achieve the object delivery.
If List<object&gt is passed, the list can be strongly converted to the serializable type, and the Object type must also implement the Serializable interface

(list<yourobject>) Getintent (). getserializable (Key)

Passing class objects through Intent

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: 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.