对象必须实现Serializable,对象代码如下:
import java.io.Serializable;
import android.graphics.drawable.Drawable;
//传送的对象
public class MyApplicationInfo extends Object implements Serializable{ //Your code }
自定义:AppParcelable
import android.os.Parcel;
import android.os.Parcelable;
import com.tcad.marketassistant.vo.MyApplicationInfo;
public class AppParcelable implements Parcelable {
private MyApplicationInfo info;
public AppParcelable(Parcel source){
info = (MyApplicationInfo)source.readValue(MyApplicationInfo.class.getClassLoader());
}
public AppParcelable(MyApplicationInfo info){
this.info = info;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(info);
}
public static final Parcelable.Creator<AppParcelable> CREATOR = new Parcelable.Creator<AppParcelable>() {
public AppParcelable createFromParcel(Parcel source) {
return new AppParcelable(source);
}
public AppParcelable[] newArray(int size) {
// return new AppParcelable[size];
throw new UnsupportedOperationException();
}
};
public MyApplicationInfo getInfo(){
return info;
}
}
调用代码,发送:
AppParcelable parcelable = new AppParcelable(info);
//Info为MyApplicationInfo对象
// 发送对象
intent.putExtra("app_parcelable", parcelable);
startActivity(intent);
接收:
AppParcelable p = getIntent().getParcelableExtra("app_parcelable");
MyApplicationInfo info = p.getInfo();