Android Development notes-pass class objects through Intent, androidintent
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 objects to be passed must implement one of the above two interfaces before they 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
The following uses the most common Serializable method as an example:
Assume that the object transferred from the logon interface (Login) to the main interface (MainActivity) is the User information for logon.
First, create a serialization class: User
import java.io.Serializable;public class User implements Serializable { private int ID; private String UserName; private String PWD; public final void setID(int value) { ID = value; } public final int getID() { return ID; } public final void setUserName(String value) { UserName = value; } public final String getUserName() { return UserName; } public final void setPWD(String value) { PWD = value; } public final String getPWD() { return PWD; } }
Login form post-login PASS content
Intent intent = new Intent();intent.setClass(Login.this, MainActivity.class);Bundle bundle = new Bundle();bundle.putSerializable("user", user);intent.putExtras(bundle);this.startActivity(intent);
Acceptor
Intent intent = this.getIntent(); user=(User)intent.getSerializableExtra("user");
The above can realize object transfer.
Supplement:
If List <Object> is passed, you can convert list to Serializable type, and the object type must also implement the Serializable interface.
Intent.putExtras(key, (Serializable)list)
Receive
(List<YourObject>)getIntent().getSerializable(key)