Bundle

Bundle实现了Parcelable接口 (序列化都用Parcelable不用Serializable Parcelable是专用于Android的)

四大组件的三个:Activity Service Receiver 都支持Intent传递Bundle  而ContentProvider天生就是用于多进程通信的
 
Parcelable序列化的典型方式
public class UserParcelable implements Parcelable{
    
    public int userId;
    public String userName;
    public boolean isMale;
    
    public BookParcelable book;
    
    public UserParcelable(int userId, String userName, boolean isMale) {
        this.userId = userId;
        this.userName = userName;
        this.isMale = isMale;
        book = new BookParcelable("android");
    }

    @Override
    public int describeContents() {
        // 这里几乎都是返回0 仅当当前对象存在对象描述符时才返回1
        return 0;
    }

    /**
     * 序列化---写
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(userId);
        dest.writeString(userName);
        dest.writeInt(isMale ?1:0);
        dest.writeParcelable(book, 0);
    }

    /**
     * 反序列化---读
     */
    public static final Parcelable.Creator<UserParcelable> CREATOR = new Creator<UserParcelable>() {
        
        @Override
        public UserParcelable[] newArray(int size) {
            return new UserParcelable[size];
        }
        
        @Override
        public UserParcelable createFromParcel(Parcel source) {
            return new UserParcelable(source);
        }
    };
    

    private UserParcelable(Parcel source){
        userId = source.readInt();
        userName = source.readString();
        isMale = source.readInt()==1;
        //book是另一个序列化对象,反序列化需要传递当前线程的上下文类加载器
        book = source.readParcelable(Thread.currentThread().getContextClassLoader());
    }
}
public class BookParcelable implements Parcelable{
    public String name;

    public BookParcelable(String name) {
        super();
        this.name = name;
    }

    @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);
        
    }
    public static final Parcelable.Creator<BookParcelable> CREATOR = new Creator<BookParcelable>() {
        
        @Override
        public BookParcelable[] newArray(int size) {
            // TODO Auto-generated method stub
            return new BookParcelable[size];
        }
        
        @Override
        public BookParcelable createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new BookParcelable(source);
        }
    };
    
    private BookParcelable(Parcel source){
        name = source.readString();
    }
}
BookParcelable.java

 传递

AActivity.java

    Intent intent = new Intent(AActivity.this, BActivity.class);
    Bundle bundle = new Bundle();
    bundle.putParcelable("user", new UserParcelable(12, "erge", true));
    intent.putExtras(bundle);
    startActivity(intent);

接收

BActivity.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        UserParcelable user = bundle.getParcelable("user");
        Toast.makeText(this, user.userId+"::"+user.userName+"::"+user.isMale+"::"+user.book.name+"::", Toast.LENGTH_LONG).show();
    }
原文地址:https://www.cnblogs.com/erhai/p/5405334.html