不时的回过头来看看自己的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. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    登录服务器,使用root用户连接mysql时出现错误提示: $ bin/mysql -uroot -p Enter password: ERROR (HY000): Can't connect to ...

  2. [Arc063F] Snuke's Coloring 2

    [Arc063F] Snuke's Coloring 2 题目大意 给你一个网格图,一些点上有标记,求边长最大空白矩形. 试题分析 专门卡\(\log^2 n\)系列. 首先由题意我们可以找到答案的下 ...

  3. java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx

    从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...

  4. flex sqlite 操作blog 二进制数据

    1,              通常的操作方式: 首先我们建立表:CREATE TABLE "pages" ("id" varchar, "data& ...

  5. 利用Hog特征和SVM分类器进行行人检测

    在2005年CVPR上,来自法国的研究人员Navneet Dalal 和Bill Triggs提出利用Hog进行特征提取,利用线性SVM作为分类器,从而实现行人检测.而这两位也通过大量的测试发现,Ho ...

  6. Android WebView H5开发拾遗

    上篇介绍了一些WebView的设置,本篇为一些补充项. 首先加载HTML5的页面时,会出现页面空白的现象,原因是需要开启 DOM storage API 功能: webSettings.setDomS ...

  7. Linux High Availabi RHCS

    -----本文大纲 简介 术语 环境配置 实现过程 命令行管理工具 ------------- 一.简介 RHCS 即 RedHat Cluster Suite ,中文意思即红帽集群套件.红帽集群套件 ...

  8. 【Eclipse提高开发速度-插件篇】安装多语言Propertys编辑工具

    安装的的时候能够通过 Help>Eclipse Marketplace的 搜索方式安装 或者 Help>Install New Software Name:Propertys URL:ht ...

  9. Android实现两次按下返回键退出

    @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BAC ...

  10. 扩展redisTemplate实现分布式锁

    原文:https://blog.csdn.net/qq1010267837/article/details/79697572 依赖jar包 compile group: 'redis.clients' ...