Transfer objects between android pages and android pages

Source: Internet
Author: User
Tags object serialization

Transfer objects between android pages and android pages

Android provides two methods for passing objects:

One is Serializable and Parcelable.

For the first method:

Import java. io. Serializable;


Public class ShopData implements Serializable {
Public String shopId;
Public String name;
Public String url;
Public String info;


Public String getShopId (){
Return shopId;
}


Public void setShopId (String shopId ){
This. shopId = shopId;
}


Public String getName (){
Return name;
}


Public void setName (String name ){
This. name = name;
}


Public String getUrl (){
Return url;
}


Public void setUrl (String url ){
This. url = url;
}


Public String getInfo (){
Return info;
}


Public void setInfo (String info ){
This.info = info;
}


}

When the value is passed:

<span style="white-space:pre"></span>Intent intent = new Intent();ShopData shopData = (ShopData)mAdapter.getItem(position);//intent.putExtra("detail", shopData);Bundle bundle = new Bundle();bundle.putSerializable("shop", shopData);intent.putExtra("detail", shopData);intent.setClass(MainActivity.this, DetailShopActivity.class);MainActivity.this.startActivity(intent);
When the value is set:

<span style="white-space:pre"></span>Intent intent = getIntent();//ShopData data = intent.getParcelableExtra("detail");ShopData data = (ShopData)intent.getSerializableExtra("detail");

For the second method:

import android.os.Parcel;import android.os.Parcelable;public class ShopData implements Parcelable{public String shopId;public String name;public String url;public String info;public ShopData(Parcel in) {readFromParcel(in);}public ShopData() {}public static final Parcelable.Creator<ShopData> CREATOR = new Parcelable.Creator<ShopData>() {public ShopData createFromParcel( Parcel in ){return new ShopData(in);}public ShopData[] newArray( int size){return new ShopData[size];}};@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(shopId);dest.writeString(name);dest.writeString(url);dest.writeString(info);}public void readFromParcel( Parcel in ){shopId = in.readString();name = in.readString();url = in.readString();info = in.readString();}}

Write as follows:

<span style="white-space:pre"></span>Intent intent = new Intent();ShopData shopData = (ShopData)mAdapter.getItem(position);intent.putExtra("detail", shopData);intent.putExtra("detail", shopData);intent.setClass(MainActivity.this, DetailShopActivity.class);MainActivity.this.startActivity(intent);


When the value is set:

<span style="white-space:pre"></span>Intent intent = getIntent();ShopData data = intent.getParcelableExtra("detail");




Set of objects passed in Android

Intent intent =...; // intent Definition
ArrayList <User> list =...; // list Value assignment
Intent. putExtra ("userList", list. toArray ());
StartActivity (intent );
The premise is that the User in the list must be implements Serializable

Receiving:
Intent intent = this. getIntent ();
Object [] cobjs = (Object []) intent. getSerializableExtra ("userList ");
For (int I = 0; I <cobjs. length; I ++ ){
User user = (User) cobjs [I];
UserList. add (user );
}
Reference: www.qingzhui.com/blog_article.php? Id = 7
 
How android uses Intent to transmit objects

PutSerializable implements object transmission between activities through object serialization and deserialization, so this method gets two completely different objects (the object obj in Activity2 is equivalent to a new object, and then the object obj value in Activity1 is assigned to it, so they have the same content, but different objects .).
Note: The object obj passed through the above method must implement the Serializable interface!

Generally, objects are not transmitted between activities. serialization and deserialization do not have any drawbacks (personal feeling). Haha... and all objects must implement the Serializable interface.

If you must upload objects, you can create some static classes or objects!

Hope to help you!

Related Article

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.