Methods for passing objects between activities

Source: Internet
Author: User

Summarize the methods for passing objects between activities.

First, we encapsulate the attributes inside our JavaBean and use the parcelable interface to implement the transfer. below is the code of my class:

Public class usersinfo implements parcelable {private long userid; private string username; // user name private string userpassword; // User Password private int usersex; // user gender private string useraddress; // user address private int userage; // user age private string userlikes; // user interests private string usermark; // user description private string userphonenumber; // user's mobile phone number private string userimgicon; // user profile private int userstatus; // user status private double userx; // user longitude private double usery; // user latitude private string userupdatetime; // user update time private string userimei; public long getuserid () {return userid;} public void setuserid (long userid) {This. userid = userid;} public int getuserstatus () {return userstatus;} public void setuserstatus (INT userstatus) {This. userstatus = userstatus;} public int getusersex () {return usersex;} public void setusersex (INT usersex) {This. usersex = usersex;} Public String getuserimgicon () {return userimgicon;} public void setuserimgicon (string userimgicon) {This. userimgicon = userimgicon;} Public String getuserupdatetime () {return userupdatetime;} public void setuserupdatetime (string userupdatetime) {This. userupdatetime = userupdatetime;} public double getuserx () {return userx;} public void setuserx (double userx) {This. userx = userx;} public double getusery () {return usery;} public void setusery (double usery) {This. usery = usery;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String getuserpassword () {return userpassword;} public void setuserpassword (string userpassword) {This. userpassword = userpassword;} Public String getuseraddress () {return useraddress;} public void setuseraddress (string useraddress) {This. useraddress = useraddress;} public int getuserage () {return userage;} public void setuserage (INT userage) {This. userage = userage;} Public String getuserlikes () {return userlikes;} public void setuserlikes (string userlikes) {This. userlikes = userlikes;} Public String getusermark () {return usermark;} public void setusermark (string usermark) {This. usermark = usermark;} Public String getuserphonenumber () {return userphonenumber;} public void setuserphonenumber (string userphonenumber) {This. userphonenumber = userphonenumber;} Public String getuserimei () {return userimei;} public void setuserimei (string userimei) {This. userimei = userimei;} @ overridepublic int describecontents () {// todo auto-generated method stubreturn 0 ;}@ overridepublic void writetoparcel (parcel DEST, int arg1) {// the Dest cannot be misplaced. writelong (userid); DeST. writestring (username); DeST. writestring (userpassword); DeST. writeint (usersex); DeST. writestring (useraddress); DeST. writeint (userage); DeST. writestring (userlikes); DeST. writestring (usermark); DeST. writestring (userphonenumber); DeST. writestring (userimgicon); DeST. writeint (userstatus); DeST. writedouble (userx); DeST. writedouble (usery); DeST. writestring (userupdatetime); DeST. writestring (userimei);} public static final parcelable. creator <usersinfo> creator = new creator <usersinfo> () {@ overridepublic usersinfo createfromparcel (parcel source) {// todo auto-generated method stub usersinfo info = new usersinfo (); info. userid = source. readlong (); info. username = source. readstring (); info. userpassword = source. readstring (); info. usersex = source. readint (); info. useraddress = source. readstring (); info. userage = source. readint (); info. userlikes = source. readstring (); info. usermark = source. readstring (); info. userphonenumber = source. readstring (); info. userimgicon = source. readstring (); // info. userimgicon = source. createbytearray (); info. userstatus = source. readint (); info. userx = source. readdouble (); info. usery = source. readdouble (); info. userupdatetime = source. readstring (); info. userimei = source. readstring (); Return Info;} @ overridepublic usersinfo [] newarray (INT size) {// todo auto-generated method stub return New usersinfo [size] ;}};}

The describecontents () method is not required. The writetoparcel method must be the same as the attributes encapsulated above, parcelable. creator <usersinfo> creator implements the same method. Finally, the newarray method returns return New usersinfo [size];
The following is the call code:

Bundle mbundle = new bundle ();
Intent = new intent (aati;. this, bati;. class );

Usersinfo user = equals to the object you want to pass;

Mbundle. putparcelable ("userobject", user );
Intent. putextras (mbundle );
Startactivity (intent );

In this way, the pass is passed. The same is true for retrieving this object in bativity, as shown in the following code:

Usersinfo = (usersinfo) getintent (). getparcelableextra ("userobject ");

In this way, all the above Code is obtained, hoping to help friends. Of course, there is another method. It seems to be through serialization. There is no specific research. If a friend thinks this method is better than mine, I hope you can share it and explore it together.

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.