Android——Bundle浅析

Bundle主要用于传输数据,以key-value的形式存储数据。

  • Bundle常用于在Activity间传递数据
  • 当bundle传递的是对象或对象数组时,必须实现Serializable或Parcelable接口,

下面分别介绍bundle在activity间如何传递基本数据类型和对象。

传递方法:

在这里插入图片描述

举个例子:

写数据:

	Bundle bundle=new Bundle();
    bundle.putString("name","police");
    bundle.putInt("years",8);
    final Intent intent=new Intent().setClassName("police.myapp","police.myapp.Main2Activity");
    intent.putExtras(bundle);
    startActivity(intent);

执行后将bundle绑定到intent,传递到Mian2Activity
读数据:

    Bundle bundle=this.getIntent().getExtras();
    String bundleString=bundle.getString("name");
    int bundleInt=bundle.getInt("years");
    textView.setText(bundleString+bundleInt);

Parcelable类型的对象
Parcelable是Android自定义的一个接口,它包括将数据写入Parcel和从Parcel中读出的API。一个实体(用类来表示),如果需要封装到Bundle中去,可以通过实现Parcelable接口来完成。
在这里插入图片描述

public interface Parcelable {  
    //内容描述接口,基本不用管  
    public int describeContents();  
    //写入接口函数,打包  
    public void writeToParcel(Parcel dest, int flags);  
    //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。  
    //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。  
    public interface Creator<T> {  
        public T createFromParcel(Parcel source);  
        public T[] newArray(int size);  
 }  
}  

1.实现Parcelable接口

看看一个官方给出的例子:

注意:对象中定义的CREATOR不可以改为其他名字噢

public class MyParcelable implements Parcelable {
    private int mData;

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };

    private MyParcelable(Parcel in) {
        mData = in.readInt();
    }
}

这个例子中的对象只包含了一个属性mData,我们再看一个例子

public class User implements Parcelable {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    protected User(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.传递Parcelable对象并解析

传递:

User user = new User("shellhub", 23);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(KEY, user);
startActivity(intent);

解析:

User user = getIntent().getExtras().getParcelable(KEY);

因为方法实现了泛型,所以不需要类型转换
public T getParcelable(@Nullable String key) {

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yawn__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值