Intent value-to-pass object jump

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.