Android系统之Intent传递数据的类型

1. 基本数据类型

存数据:

Intent it = new Intent(this, SecondActivity.class);
XXX value = xxx;
it.putExtra("key", value);
startActivity(it);

取数据:

int value = getIntent().getXXXExtra("key");

例如:
存数据:

Intent it = new Intent(FirstActivity.this, SecondActivity.class);
int value = 2;
it.putExtra("key", value);
startActivity(it);

取数据:

int value = getIntent().getIntExtra("key");

2. Bundle

存数据:

Intent it = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bd = new Bundle();
bd.putInt("num", 1);
bd.putString("detail", "hello");
it.putExtras(bd);
startActivity(it);

取数据:

Bundle bd = getIntent().getExtras();
int value = bd.getInt("num");
String str = bd.getString("detail");

3. Serializable和Parcelable

3.1 Serializable

Person定义:

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

存数据:

Person person = new Person();  
person.setName("Tom");  
person.setAge(20);  
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);  
intent.putExtra("person_data", person);  
startActivity(intent); 

取数据:

Person person = (Person) getIntent().getSerializableExtra("person_data");

3.2 Parcelable

Person定义:

public class Person implements Parcelable{
	private String name;
	private int age;
	public void setName(String name) {
        this.name = name;
    }
	public String getName() {
        return name;
    }
	public void setAge(int age) {
        this.age = age;
    }
	public int getAge() {
        return age;
    }
	//这里的读的顺序必须与writeToParcel(Parcel dest, int flags)方法中写的顺序一致
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Person() {
        super();
    }
    //这里的读的顺序必须与writeToParcel(Parcel dest, int flags)方法中写的顺序一致,否则数据会有差错
    public Person(Parcel source) {
        name = source.readString();
        age = source.readInt();
    }
    //这里默认返回0即可
    @Override
    public int describeContents() {
        return 0;
    }
    //把值写入Parcel中
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
    public static final Creator<Person> CREATOR = new Creator<Person>() {
        //供外部类反序列化本类数组使用
        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
        //从Parcel中读取数据
        @Override
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }
    };
}

存数据://和Serializable相同

Person person = new Person();  
person.setName("Tom");  
person.setAge(20);  
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);  
intent.putExtra("person_data", person);  
startActivity(intent); 

取数据://和Serializable的区别是函数名不同

Person person = (Person) getIntent().getParcelableExtra("person_data");

好文:https://www.jianshu.com/p/1169dba99261

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值