不时的回过头来看看自己的Andriod学习、实践之路,总发现有些曾经不明确的,如今清楚缘由。也会发现一些之前没怎么关注的。如今看到了 ,很想去深刻了解的。

比方:Bundle。

在一个Activity的生命周期中,首先要运行的是onCreate方法

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_modifyheadphoto);
}

在默认情况下,上面红色部分 是 onCreate方法的參数 , 默认运行的方法, 都会自己主动加入,而这部分普通情况。我都不去关注。你呢?

今天,就来搞清楚这个Bundle的作用,以及和Intent的差别。

一、Bundle:A mapping from String values to various Parcelable types

键值对的集合

类继承关系:

java.lang.Object

     android.os.Bundle



Bundle类是一个final类:

public final class Bundle extends Objectimplements Parcelable Cloneable

作用:能够用作是两个Activity间的通讯。

使用方法:

①、装载数据:

Bundle mBundle = new Bundle();

mBundle.putString("DataTag", "要传过去的数据");

Intent intent = new Intent(); 


         intent.setClass(MainActivity.this, Destion.class);  

         intent.putExtras(mBundle);

②、目标Activity解析数据

Bundle bundle = getIntent().getExtras();  //得到传过来的bundle

String data = bundle.getString("DataTag");//读出数据

二、Intent的含义和作用 就略了。

。。直接上二者比較:

两个Activity之间传递数据,数据的附加有两种方式:

    一种是直接 intent.putxx();

     还有一种是  先bundle.putxx(), 然后再调用public Intent putExtras (Bundle extras)  加入bundle.

事实上两种的本质是一样的。

先看Intent的方法:

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传值的话最后调用的方法:Intent.putExtras(Bundle extras):

public Intent putExtras(Bundle extras) { 

    if (mExtras == null) { 

        mExtras = new Bundle(); 

    } 

    mExtras.putAll(extras); 

    return this; 



能够看到。事实上是把之前那个bundle中的数据批量加入到intent内部的bundle中。

事实上是和上面的直接用Intent传键值对是一样的原理。

总之呢,Intent旨在数据传递,bundle旨在存取数据,当然intent也提供一部分数据的存取,但比起bundle就显得不专业。不灵活的多

Android-Bundle认知、和Intent的差别的更多相关文章

  1. android学习之路--------intent

    正式开始学习android,没有看书和视频,所以没有系统的学,只是看到哪个知识点就去学习,今天学习界面之间的跳转,以及传值,主要的知识点是intent, @Override protected voi ...

  2. android bundle存放数据详解

    转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...

  3. Android Bundle类

    根据google官方的文档(http://developer.android.com/reference/android/os/Bundle.html) Bundle类是一个key-value对,“A ...

  4. android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...

  5. Android开发-API指南-Intent和Intent过滤器

    Intents and Intent Filters 英文原文:http://developer.android.com/guide/components/intents-filters.html 采 ...

  6. Android组件的通讯——Intent

    转载:Android组件的通讯-Intent 1.概述 一个应用程序的三个核心组件——activities.services.broadcast receivers,都是通过叫做intents的消息激 ...

  7. Android 开发笔记——通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  8. Android开发学习之Intent具体解释

    Intent简单介绍和具体解释:           Intent:协助应用间的交互与通信,Intent负责相应用中一次操作的动作.动作涉及的数据.附加数据进行描写叙述.               ...

  9. Android应用开发中Intent的作用及使用方法

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

随机推荐

  1. bzoj1477 poj1061 青蛙的约会

    Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...

  2. ProtoBuffer使用笔记

    ProtoBuffer是由谷歌研发的对象序列化和反序列化的开源工具,ProtoBuffer和Xml类似,都是数据描述工具,后者使用更为广泛,前者Google内部使用且具有更高的效率.该工具安装和使用都 ...

  3. CSS3制作ajax loader icon

    demo 本文用到的两个CSS3属性:transform.animation 一.HTML <div class="ajax-loading"> <div cla ...

  4. NBT(NetBIOS Over TCP)名称解析概述

    在微软IP网络中,客户计算机查找其他计算机并与之进行通信的主要手段是利用域名(DNS).但是,使用先前版本的Windows户机也使用NetBIOS协议,将名称解析为IP地址. 通过三种方法解析NetB ...

  5. 小程序navigator点击有时候会闪一下

    <navigator hover-class="none">

  6. perf 工具介绍2

    [root@localhost ~]# cat test1.c void longa() { int i,j; ; i < ; i++) j=i; //am I silly or crazy? ...

  7. HTTP的KeepAlive是开启还是关闭?

    HTTP的KeepAlive是开启还是关闭? http://itindex.net/detail/50719-http-keepalive 1.KeepAlive的概念与优势 HTTP的KeepAli ...

  8. Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 2012 Backup

    Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 201 ...

  9. Centos6.5下Oracle 11g R2安装过程

    1准备 CentOS-6.5-x86_64-bin-DVD1 linux_11gR2_database_1of2 linux_11gR2_database_2of2 VMware Workstatio ...

  10. C#中的IDisposable模式

    当谈到垃圾回收,在C#中,托管资源的垃圾回收是通过CLR的Garbage Collection来实现的,Garbage Collection会调用堆栈上对象的析构函数完成对象的释放工作:而对于一些非托 ...