Use Intent to transfer Java objects between activities in Android projects -- Implement Parcelable Interface
Using Intent to implement Java object transfer between activities in Android projects, there are two ways: one is the passed object implements the Serializable interface; the other is the passed object implements the Parcelable interface, this blog summarizes how to implement Java object transfer when passing objects to implement the Parcelable interface:
Code 1: Add a file named "User. java:
Package com. ghj. vo; import android. OS. parcel; import android. OS. parcelable; public class User implements Parcelable {private String id; private String name; public User (String id, String name) {this. id = id; this. name = name;}/*** implements the writeToParcel method of the Parcelable interface, and writes the data to be transmitted to the Parcel "Container" to obtain data from the Parcel "container. * // @ Overridepublic void writeToParcel (Parcel out, int flags) {out. writeString (id); out. writeString (name);}/*** instantiate the static internal Object CREATOR to implement the interface Parcelable. creator. Note: either public static final or internal Object CREATOR cannot be changed and must be capitalized. */Public static final Creator
CREATOR = new Creator
() {@ Override public User [] newArray (int size) {return new User [size];}/*** read transmitted data values from Parcel "container, and encapsulated into a single User entity */@ Override public User createFromParcel (Parcel in) {return new User (in. readString (), in. readString () ;}}; public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;}/*** description, 1 Returns 0. * // @ Overridepublic int describeContents () {return 0 ;}}
Note: The above User class encapsulates the data to be transmitted into a Parcel object by implementing the writeToParcel method of the Parcelable interface, and then implements Parcelable through the internal implementation class. the createFromParcel method of the Creator interface obtains data from the Parcel object and encapsulates the data into a User object. It is worth noting that the order in which transmitted data is written to Parcel (see the writeToParcel method) must be the same as the order in which data is obtained from Parcel (see createFromParcel method.
Code 2: Add a file named "FromActivity. java:
Package com. ghj. activity; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import com. ghj. vo. user; public class FromActivity extends Activity implements OnClickListener {private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. from); button = (Button) findViewById (R. id. OK _btn); button. setOnClickListener (this) ;}@ Overridepublic void onClick (View view) {Intent intent = new Intent (FromActivity. this, ToActivity. class); Bundle bundle = new Bundle (); User user = new User ("56862586-425e-4749-93ca-440cd576ba92", "Wang Xiang"); bundle. putParcelable ("user", user); intent. putExtras (bundle); startActivity (intent );}}
Code 3: Add a file named "ToActivity. java:
package com.ghj.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;import com.ghj.vo.User;public class ToActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.to);TextView textView = (TextView)findViewById(R.id.show_user_info);Intent intent = getIntent();User user = intent.getParcelableExtra("user");textView.setText(user.getId() + ":" + user.getName());}}
Code 4: Add a file named "from. xml:
Code 5: Add a file named "to. xml:
【
Download Demo at 0]