Bundle Intent transfer or use custom objects (serialization transfer)

Bundle Intent transfer or use custom objects need to custom object serialization.

Converting a sequence of objects represents a state may be stored or transmitted. Serialized objects may be transmitted over the network, may be stored locally. Sequence of two ways: Serializable Parcelable mode and manner.

1.Serializable way
Serializable that is serialized mean, if you have a custom class is Person.java, name and age there are two fields
you want to serialize write:

public class Person implements Serializable {	
	private String name;
	private int age;
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
	public void setName(String name){
		this.name = name;
	}
	public void setAge(int age){
		this.age = age;
	}
		
}

set, get physical methods are read class generation and assignment of data, the focus is represented by the first row implements Serializable class implements Serializable interface Person, Person objects are all possible sequences of the

When data transmission:
the Intent:

//发送数据端
Person person =new Person();
person.setName("Tom");
person.setAge(20);
Intent intent = new Intent(this,NewActivity.class);//或初始化其他Intent的构造函数
intent.putExtra("key",person);//key是你定义的键值对的键值,唯一即可。
context.startActivity(intent);
//接收数据端,比如一个Activity:
Person person =(Person)getIntent().getSerializableExtra("key")

Bundle:

//发送数据端
Person person =new Person();
person.setName("Tom");
person.setAge(20);
Bundle bundle = new Bundle();
bundle.putSerializable("key",person);//key是你定义的键值对的键值,唯一即可。

//接收数据端
Person person =(Person)bundle.getSerializable("key");

Serializable is relatively simple, but the whole object will be serialized, efficiency is lower than Parcelable manner.

2.Parcelable way
Parcelable way to serialize more efficient, but the amount of code a little more.
For a little complicated example

public class MainFunctionBean implements Parcelable { //实现Parcelable 接口
    private String title;
    private int bitmapResKey;
    private boolean isLongPressed = false;
  
    public void setTitle(String title) {
        this.title = title;
    }
    
 	 public String getTitle() {
        return title;
    }
    public void setBitmapResKey(int bitmapResKey) {
        this.bitmapResKey = bitmapResKey;
    }

    public int getBitmapResKey() {
        return bitmapResKey;
    }

     public boolean isLongPressed() {
        return isLongPressed;
    }

    public void setLongPressed(boolean longPressed) {
        isLongPressed = longPressed;
    }

    @Override
    public int describeContents() { //默认覆写的方法,不用改
        return 0;
    }

    public static final Parcelable.Creator<MainFunctionBean> CREATOR = new Parcelable.Creator<MainFunctionBean>() {

        @Override
        public MainFunctionBean createFromParcel(Parcel source) { 
        //重点覆写 的方法,各个字段的声明顺序必须和	writeToParcel方法里的一致
            MainFunctionBean mainFunctionBean = new MainFunctionBean();
            mainFunctionBean.title = source.readString();
            mainFunctionBean.bitmapResKey = source.readInt();
            mainFunctionBean.isLongPressed = source.readByte() != 0;//序列化的数据恢复成boolean类型
            return mainFunctionBean;
        }

        @Override
        public MainFunctionBean[] newArray(int size) {  //默认覆写的方法,把size传进数组
            return new MainFunctionBean[size];
        }
    };

    @Override
    public void writeToParcel(Parcel dest, int flags) { //重点覆写 的方法,需要将本实体类的字段一 一写出进行序列化。
        dest.writeString(title);//title是String类型
        dest.writeInt(bitmapResKey);//bitmapResKey是int类型
        dest.writeByte((byte) (isLongPressed ? 1 : 0));//isLongPressed 是boolean类型,改写成Byte便于序列化
    }

   
}

When passing data, methods, and almost just Serializable Serializable corresponds to the word became Parcelable

Guess you like

Origin blog.csdn.net/weixin_43115440/article/details/91441980