Android-Bundle认知、和Intent的差别

不时的回过头来看看自己的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就显得不专业。不灵活的多

时间: 2025-01-04 15:45:52

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

Android Bundle类

今天发现自己连Bundle类都没有搞清楚,于是花时间研究了一下. 根据google官方的文档(http://developer.android.com/reference/android/os/Bundle.html) Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.” 类继承关系: java.lang.Object     android.os.Bundle Bundle类是一个fin

Android Bundle SharedPreferences区别

Android之Bundle传递数据详解与实例及Bundle与SharedPreferences的区别 效果如下: 初始界面 输入信息 “确定”以后 一.API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsKey(String key):返回指定key的值 getString(String key):返回指定key的字符 hasFileDescriptor

android学习之路--------intent

正式开始学习android,没有看书和视频,所以没有系统的学,只是看到哪个知识点就去学习,今天学习界面之间的跳转,以及传值,主要的知识点是intent, @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); LinearLayout view = new LinearLa

【Android】12.1 Intent基本概念

分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 Intent:意图,含义就是你想利用它调用哪个组件实现相关的功能,比如调用相机组件实现拍照.调用Contact组件获取联系人信息等. 在Android系统的四个核心组件中,除了Content provider以外,其他三个核心组件(Activity.Services.Broadcast receiver)实际上都是被一个叫做Intent的异步消息来激活的. 通过传递Intent对象调用的这些组件功能时,这些组件

Android Bundle类,通过bundle实现在两个activity之间的通讯

根据google官方的文档(http://developer.android.com/reference/android/os/Bundle.html) Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.” 类继承关系: java.lang.Object     android.os.Bundle Bundle类是一个final类:public final classBundleexten

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

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递. Intent中传递这2种对象的方法: Bundle.putSerializable(Key,Object); //实现Serializable接口的对象 Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象 以下

Android学习笔记-Activity&Intent

界面activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="matc

Android组件的通讯——Intent

转载:Android组件的通讯-Intent 1.概述 一个应用程序的三个核心组件——activities.services.broadcast receivers,都是通过叫做intents的消息激活.Intent消息是一种同一或不同应用程序中的组件之间延迟运行时绑定的机制.intent本身(是一个Intent对象),是一个被动的数据结构保存一个将要执行的操作的抽象描述,或在广播的情况下,通常是某事已经发生且正在宣告.对于这三种组件,有独立的传送intent的机制: Activity:一个in

Android Bundle类别

即使在今天发现自己Bundle类不明确,因此,花时间去研究了一下. 依据google官方文件(http://developer.android.com/reference/android/os/Bundle.html) Bundle类是一个key-value对,"A mapping from String values to various Parcelable types." 类继承关系: java.lang.Object android.os.Bundle Bundle类是一个fi