android中的组件间传递的对象一般实现Parcelable接口,当然也可以使用java的Serializable接口,前者是android专门设计的,效率更高,下面我们就来实现一个Parcelabel。
1. 创建一个类实现Parcelable接口,具体实现如下:
- package com.hebaijun.testparcelable;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class ParcelableData implements Parcelable{
- private String name;
- private int age;
- public ParcelableData(){
- name = "guest";
- age = 20;
- }
- public ParcelableData(Parcel in){
- //顺序要和writeToParcel写的顺序一样
- name = in.readString();
- age = in.readInt();
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public int getAge(){
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- // TODO Auto-generated method stub
- dest.writeString(name);
- dest.writeInt(age);
- }
- public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {
- public ParcelableData createFromParcel(Parcel in) {
- return new ParcelableData(in);
- }
- public ParcelableData[] newArray(int size) {
- return new ParcelableData[size];
- }
- };
- }
package com.hebaijun.testparcelable;
import android.os.Parcel;
import android.os.Parcelable;
public class ParcelableData implements Parcelable{
private String name;
private int age;
public ParcelableData(){
name = "guest";
age = 20;
}
public ParcelableData(Parcel in){
//顺序要和writeToParcel写的顺序一样
name = in.readString();
age = in.readInt();
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeInt(age);
}
public static final Parcelable.Creator<ParcelableData> CREATOR = new Parcelable.Creator<ParcelableData>() {
public ParcelableData createFromParcel(Parcel in) {
return new ParcelableData(in);
}
public ParcelableData[] newArray(int size) {
return new ParcelableData[size];
}
};
}
2. 通过下面的方法发送对象。Bundle类也实现了Parcelable接口,一般在android中我们是通过Bundle来封装数据并进行传送的。
- Intent intent = new Intent();
- intent.setClass(this, SubActivity.class);
- // 直接添加
- //intent.putExtra("MyData", new ParcelableData());
- // 通过Bundle
- Bundle bundle = new Bundle();
- bundle.putString("MyString", "test bundle");
- bundle.putParcelable("MyData", new ParcelableData());
- intent.putExtras(bundle);
- startActivity(intent);
Intent intent = new Intent();
intent.setClass(this, SubActivity.class);
// 直接添加
//intent.putExtra("MyData", new ParcelableData());
// 通过Bundle
Bundle bundle = new Bundle();
bundle.putString("MyString", "test bundle");
bundle.putParcelable("MyData", new ParcelableData());
intent.putExtras(bundle);
startActivity(intent);
3. 下面的接收对象的方法。
- //ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");
- Bundle bundle = getIntent().getExtras();
- ParcelableData parcelableData = bundle.getParcelable("MyData");
- String testBundleString = bundle.getString("MyString");
- Log.v("string=", testBundleString);
- Log.v("name=", parcelableData.getName());
- Log.v("age=", ""+parcelableData.getAge());
//ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");
Bundle bundle = getIntent().getExtras();
ParcelableData parcelableData = bundle.getParcelable("MyData");
String testBundleString = bundle.getString("MyString");
Log.v("string=", testBundleString);
Log.v("name=", parcelableData.getName());
Log.v("age=", ""+parcelableData.getAge());