Activity passes values between intent, supports basic data types and string objects and their array objects byte, byte[], Char, char[], Boolean, boolean[], short, short[], int, int [], Long, long[], float, float[], double, double[], String, string[], and the class object that implements the serializable and Parcelable interfaces.
Package Com.example.test;import Android.os.bundle;import Android.app.activity;import android.content.intent;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; public class Mainactivity extends Activity implements Onclicklistener{private Button btnsend; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btnsend = (Button) Findviewbyid (r.id.send); Btnsend.setonclicklistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic void OnClick (View v) {if (V.getid () = = R.id.send) {Intent Intent = new Intent (this, homeactivity.class); Bundle bundle = new bundle (); Bundle.putstring ("Data", "Hello ....."); User user = new user (); User.setage (11); User.setname ("Xiaosi"); Bundle.putserializable ("user", user); Intent.Putextras (bundle); StartActivity (Intent); }}}
Package Com.example.test;import Android.os.bundle;import Android.app.activity;import android.content.intent;import Android.view.menu;import Android.widget.textview;public class Homeactivity extends Activity {private TextView text;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_home); Intent intent=getintent (); Bundle Bundle=intent.getextras (); String str=bundle.getstring ("Data"); User user = (user) bundle.getserializable ("user"); text= (TextView) Findviewbyid (r.id.text); Text.settext (str+ "Name:" +user.getname () + "Age:" +user.getage ()); } @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (r.menu.home, menu); return true;}}
Therefore, the transfer of custom data between the activity, can only implement implements serializable or Parcelable interface.
You can use bundle putserializable (string,serizlizable) data or directly with intent PUTEXTRR (string,serizlizable) data.
I have a need to pass multiple custom type data, so I wrote an object that encapsulates the custom data I need, which implements the Serizlizable interface. But there was a mistake in the Putextra.
Later I found that to pass data between the activity must be all the content of the implementation of the serializable interface, and my object encapsulated in a custom type of data does not implement this interface, so an error occurred.
Then I let that custom type implement the Serializable interface and there's no problem.
I have customized a Transmitdata object that implements the serializable interface.
Package Com.sjf.model;import Java.io.serializable;import java.util.date;/** * Transmits objects directly through the socket to the maximum object * @author Xiaosi * */public class Transmitdata implements serializable{/** * */private static final long Serialversionuid = 1l;//sent message type PR Ivate Transmittype type;//from which user private string fromuser;//to which user private string touser;//the content of the transfer private user user;//request result p Rivate Boolean issucess;//Request description, Feedback description Private String description;//Request or feedback time private date Time;public date GetTime () {return Time;} public void SetTime (Date time) {this.time = time;} public Boolean issucess () {return issucess;} public void Setsucess (Boolean issucess) {this.issucess = issucess;} Public String GetDescription () {return description;} public void SetDescription (String description) {this.description = description;} Public Transmittype GetType () {return type;} public void SetType (Transmittype type) {this.type = type;} Public String Getfromuser () {return fromuser;} public void Setfromuser (String fromuser) {this.fromuser = Fromuser;}Public String Gettouser () {return touser;} public void Settouser (String touser) {this.touser = Touser;} Public User GetUser () {return user;} public void SetUser (user user) {this.user = user;}}
However, the custom data type in the Transmitdata object user does not implement the serialization interface.
The error is: Parcelable encountered IOException writing serializable object (name = XXX)
[Android] Parcelable encountered IOException writing serializable object (name = XXX)