Activity Intent 传递参数之 Serializable

一、创建 继承 Serializable的bean,增加一个唯一个序列化id  serialVersionUID

作用是因为java  sdk会自动进行hash计算,并生成唯一的UID值。手动设置serialVersionUID的好处是当前class如果改变了成员变量,比如增加或者删除之后,这个UID是不改变的,那么反序列化就不会失败

例如:

public class BaseBean implements Serializable {
private static final long serialVersionUID = 1L;
public static final int TO_SEND = 0;
public static final int SEND_REVIEW = 1;
public static final int BATCH_OPERATION = 2;

private int mType;
private int mState;

public BaseBean() {

}

/**
* Structure function.
*
* @param type list type ex:TO_SEND.
* @param state select state.
*/
public BaseBean(int type, int state) {
this.mType = type;
this.mState = state;
}

public int getType() {
return mType;
}

public void setType(int type) {
this.mType = type;
}

public int getState() {
return mState;
}

public void setState(int state) {
this.mState = state;
}
}

二、第一个activity 发送 Serializable对象
BaseBean baseBean = new BaseBean();
baseBean.setState(1);
Intent intent = new Intent(this, BatchOperationActivity.class);
intent.putExtra("baseBean", baseBean);
startActivity(intent);

三、第二个activity 接收 Serializable对象
BaseBean baseBean = (BaseBean) intent.getSerializableExtra("baseBean");
原文地址:https://www.cnblogs.com/adamli/p/13228933.html