Android Bundle详解

1.Bundle简介:

Bundle主要用于传输数据,它保存的数据,是以key-value的形式存储的。
Bundle常用于在Activity间传递数据 ,当不bundle传递的是对象或对象数组时,必须实现Serializable或Parcelable接口,下面分别介绍bundle在activity间如何传递基本数据类型和对象。

2.传递基本类型

Bundle提供了各种putXxx()/getXxx()方法,用于读写基本数据类型,Bundle用于读写基本数据类型的API有:
Android Bundle详解

示例
写数据的方法:

                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

读数据的方法:
(Intent.getExtras()获取bundle对象)

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

3.传递Parcelable类型的对象

3.1Parcelable说明

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

4.传递Serializable类型的对象

请看原文

转自:https://blog.csdn.net/suncherrydream/article/details/52974500

转载于:https://blog.51cto.com/13652962/2354063

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值