Intent value-to-pass object jump
1. Transfer value
Storing a field in the original activity
Intent = new Intent (from.this, to.class);
Intent.putextra ("Switch", "Chongzhi");
StartActivity (Intent);
Jump to new activity after Q to remove the field
Intent switchintent = Getintent ();
String myswitch = Switchintent.getstringextra ("switch");
2. Passing objects
Intent = dialoginfo.getintent ();
/passes an object to the intent object
UserInfo need to implement Parcelable interface creation creator
stored in a bundle
Bundle Mybundle = new bundle ();
Mybundle.putparcelable ("UserInfo", userInfo);
Intent.putextras (Mybundle);
Take an object in a new toactivity
Intent getintent = Getintent ();
UserInfo userinfo= (UserInfo) (Getintent.getparcelableextra ("UserInfo"));
See http://caiwb1990.iteye.com/blog/1404201 for details.
3. An interface class that implements Parcelable is also a serialization of the object
Package com.example.entity;
Import Android.os.Parcel;
Import android.os.Parcelable;
Import Android.os.Parcelable.Creator;
public class UserInfo implements parcelable{
private int id;
Private String username;
private String password;
Private String Phonenum;
private double money;
@Override
public int describecontents () {
TODO auto-generated Method Stub
return 0;
}
@Override
public void Writetoparcel (Parcel dest, int arg1) {
TODO auto-generated Method Stub
Dest.writestring (username);
dest.writestring (password);
Dest.writestring (Phonenum);
Dest.writedouble (Money);
}
public static final creator<userinfo> Creator = new Creator<userinfo> () {
@Override
Public UserInfo Createfromparcel (Parcel source) {
UserInfo entity = new UserInfo ();
// The order must be the same as the write order
Entity.username= source.readstring ();
Entity.password = Source.readstring ();
Entity.phonenum = Source.readstring ();
Entity.money = Source.readdouble ();
return entity;
}
@Override
Public userinfo[] NewArray (int arg0) {
TODO auto-generated Method Stub
return null;
}
};
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Intent value-to-pass object jump