Passing class objects through Intent
In Android, Intent provides two methods for passing objects: one is to pass objects through the Serializable interface and the other is to pass objects through the Parcelable interface.
The object to be passed must implement one of the above two interfaces before it can be passed directly through Intent
Methods for passing these two objects in Intent:
Bundle. putSerializable (Key, Object); // The Bundle. putParcelable (Key, Object) Object that implements the Serializable interface; // the Object that implements 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;} }The Intent code sent by the Registration Form:
@ 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 // The three components obtained EditText name = (EditText) findViewById (R. id. name); EditText password = (EditText) findViewB YId (R. id. password); RadioButton male = (RadioButton) findViewById (R. id. radio0); // determines whether the String sex = (male. isChecked ())? "Male": "female"; // encapsulate the object into Person p = new Person (name. getText (). toString (), password. getText (). toString (), sex); // create a Bundle object Bundle bundle = new Bundle (); bundle. putSerializable ("person", p); // put an object in the Bundle // create an Intent object Intent intent = new Intent (MainActivity. this, ResultActivity. class); intent. putExtras (bundle); // start ActivitystartActivity (intent) corresponding to intent );}});Acceptor code:
@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_result); // obtain the display component TextView name = (TextView) findViewById (R. id. text1); TextView password = (TextView) findViewById (R. id. text2); TextView sex = (TextView) findViewById (R. id. text3); // get the Intent object Intent intent = getIntent (); // obtain the sequence data from the Intent object // Person p = (Person) intent. getSerializableExtra ("person"); equivalent to Bundle bundle = intent. getExtras (); // get Bundle object Person p = (Person) bundle. getSerializable ("person"); // get the name of the serializable object in the Bundle object. setText (p. getName (); password. setText (p. getPassword (); sex. setText (p. getSex ());}The above can realize object transfer.
If List is passed To convert the list type to the Serializable type, and the object type must also implement the Serializable interface.
Intent.putExtras(key, (Serializable)list)
(List
)getIntent().getSerializable(key)