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
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;} }
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 (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);//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> 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