Intent和Bundle的区别
安卓 Intent (1)数据传递 Intent和Bundle详解
这几天在写android小程序的时候碰到了bundle,突然发现还不清楚intent和bundle之间的关系,决定百度google之。后来发现很多都在说用法,没提到什么本质区别的,所以去翻源码- -
首先来看下intent:
public Intent putExtra(String name, boolean value);public Intent putExtra(String name, byte value);public Intent putExtra(String name, char value);public Intent putExtra(String name, short value);public Intent putExtra(String name, int value);public Intent putExtra(String name, long value);public Intent putExtra(String name, float value);public Intent putExtra(String name, double value);public Intent putExtra(String name, String value);public Intent putExtra(String name, CharSequence value);public Intent putExtra(String name, Parcelable value);public Intent putExtra(String name, Parcelable[] value);...
intent内部定义了很多put方法,功能都是把key-val存进来。我们来看一个具体put函数的内部实现:
public Intent putExtra(String name, boolean value) {if (mExtras == null) {mExtras = new Bundle();}mExtras.putBoolean(name, value);return this;}
其中mExtras是intent内部定义的一个private Bundle变量。
大家可以看到,intent其实是调用了bundle相应的put函数,也就是说,intent内部还是用bundle来实现数据传递的,只是封装了一层而已。
再来说Bundle:
public void putBoolean(String key, boolean value);public void putByte(String key, byte value);public void putChar(String key, char value);...
put函数都是分类型的。
再来看用法:
只用intent:类型什么的是不需要你来操心的,你只需要putExtra就好了,内部会都存在一个bundle对象中。key-val对是一个一个被加进去的。
用intent和bundle:key-val对先被一个个的加到bundle里面,再把这个bundle put到intent中,其中用了下面这个函数:
public Intent putExtras(Bundle extras) {if (mExtras == null) {mExtras = new Bundle();}mExtras.putAll(extras);return this;}
可以看到,其实是把之前那个bundle中的数据批量添加到intent内部的bundle中。
取数据的时候,可以一个个的取出来(这个不赘述了),也可以把数据打包一起取出来:
public Bundle getExtras() {return (mExtras != null)? new Bundle(mExtras): null;}
这个函数是把当前intent中所有的数据一起打包的(假如说你既用了bundle也用了intent本身的put函数来加数据,最后用get函数返回的是bundle+其他数据一起的)。
说到底呢,区别是什么,如果你想对数据进行比较灵活的操作(批量操作什么的)的话就用bundle吧,当然你也可以getIntent之后直接添加数据然后把这个intent发送出去(当然这是不推荐的一种用法啦= =)。
还有就是,Bundle是可以对对象进行操作的,而Intent不可以。Bundle相对于Intent比较偏下层,比Intent接口更多,更灵活,但Bundle仍需要借助Intent才能在Activity之间传递。
概括一下,Intent旨在数据传递,bundle旨在存取数据,当然intent也提供一部分数据的存取,但比起bundle就显得不专业,不灵活的多。
Intent和Bundle的区别相关推荐
- android 数据传递详解(Serialization、Parcelable、Parcel、Intent、Bundle)
第一.四大组件间数据传递方式: 启动四大组件通过Intent对象来实现,Intent的功能包括启动四大组件以及相关信息+传递数据. 其中传递数据Intent提供了putExtra和对应的getExtr ...
- java intent bundle_Android 通过Intent使用Bundle传递对象详细介绍
Android 通过Intent使用Bundle传递对象 Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法. 被传递的对象需要先实现序列化, ...
- Intent、Bundle
1.Intent封装 public class IntentHelper { private Intent intent; private static IntentHelper intentHelp ...
- Android中Activity之间的数据传递(Intent和Bundle)
当一个Activity启动另一个Activity时,常常会有一些数据传过去,对于Activity之间的数据交换更简单,因为两个Activity之间进行数据传递交换更简单,因为两个Activity之间本 ...
- Android用Intent和Bundle传list
Intent intent = new Intent(); Bundle bundle = new Bundle(); ArrayList list = new ArrayList();//这个arr ...
- Android安全笔记-进程间通信基本概念(intent、bundle、Parcelable、parcel)
进程间通信 进程间传递消息: 例如启动一个Activity,在Intent中设置传递参数及其他数据(字符串.整数.数组.对象) ·intent.putExtra 消息组织和传递:Intent.Bund ...
- webpack——module、chunk和bundle的区别
在Webpack中,Module.Chunk和Bundle是三个概念. 1.Module Module(模块)是Webpack中最基本的概念,它代表着一个单独的文件(或一组相关的文件),它可以是Jav ...
- android intent bundle传递参数,Android 使用Intent和Bundle传递数据及如何传递enum
推荐文章 Settings Bundle是在自己的程序中建立的一组文件,利用它可以告诉设备中的Settings程序我们写的程序有哪些设置项.用户在Settings程序中设置好相关相关选项后回到我们自己 ...
- Intent和PendingIntent的区别
intent英文意思是意图,pending表示即将发生或来临的事情. PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. In ...
最新文章
- ASP.NET Web Pages – 页面布局简介
- django模板导入js,css等外部文件
- CentOS7部署NFS
- 后缀为hta,url,html,htm,html标签
- MOSN 多协议扩展开发实践
- Code Issues 2,637 Pull requests 0 Projects 1 Wiki Security Insights Settings 使用filter node快速找到XML f
- Linux下导出MySQL为SQL文件_MySQL导入导出.sql文件步骤
- Python 爬虫+tkinter界面 实现历史天气查询
- QRCode二维码生成方案及其在带LOGO型二维码中的应用(2)
- C# 理解Thread.Sleep()方法 ----转帖
- 服务器机柜内手机信号,手机信号强度是什么
- UBUNTU设置环境变量MALLOC_CHECK_=1检查内存
- 《工科泛函分析基础》预习笔记 证明:可测集上的连续函数都是可测函数
- gerrit/git操作中遇到的问题
- ant design vue 描述列表Descriptions数据绑定
- 【原创】三星、镁光、海力士内存颗粒命名规则摘录
- 以太坊:过去、现在、未来
- R中两种常用并行方法——2. snowfall
- 【CXY】JAVA基础 之 异常概述
- 创建新Docker容器时出现“The container name “/xxx“ is already in use by container xxxxxxxxxxx...”问题的解决办法