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();
    }

 

 

 

 

 

posted @ 2016-04-18 17:54  洱海  阅读(195)  评论(0编辑  收藏  举报
.First { margin: 10px 0; font-family: 'Microsoft Yahei'; text-align: left; padding: 6px 20px; color: #fff; background: #55895B; font-size: 20px; border-radius: 4px; clear: both; } .Second { margin: 10px 0; font-family: 'Microsoft Yahei'; padding: 6px 20px; background: #93C8A2; color: white; font-size: 18px; border-radius: 4px; clear: both; } .Third { margin: 10px 0; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0; font-size: 16px; color: black; background: #C6EFD2; border-radius: 4px; clear: both; } .note { margin: 10px 0; padding: 15px 20px 15px 60px; background: #FCFAA9 url('http://images.cnblogs.com/cnblogs_com/libaoheng/305804/o_yellow-pin.png') no-repeat 20px 0; font-size: 15px; font-family: 'Microsoft Yahei'; box-shadow: 0 0 8px #aaa; clear: both; } .demo { text-align: left; padding: 6px 20px; overflow: auto; border-radius: 4px; background: orange; color: #fff; font-size: 16px; clear: both; } .cnblogs_Highlighter { border: solid 1px #ccc; clear: both; } .cnblogs_code { background: #EFFFF4; border: solid 0px #939393; font-size: 14px; clear: both; padding: 10px 20px; } .cnblogs_code pre { font-size: 14px; } .cnblogs_code span { font-family: Courier New; font-size: 14px; }