zoukankan      html  css  js  c++  java
  • Intent传递数据之Bundle

    之前见到时直接使用,不知其理,在网上查了相关资料,算是有所了解,整理如下:

    bundle的用法:

           Bundle相当于Map类,就是一个映射,用Bundle绑定数据,便于数据处理

            它主要作用于Activity之间的数据传递. 

    两个activity之间的通讯可以通过bundle类来实现,做法就是:

    一、新建一个bundle类

    Bundle mBundle = new Bundle();   

    二、bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)

    mBundle.putString("Data", "data from TestBundle");  

    三、新建一个intent对象,并将该bundle加入这个intent对象

    Intent intent = new Intent();    

    intent.setClass(TestBundle.this, Target.class);    

    intent.putExtras(mBundle);  

    四、取出数据

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

    String data = bundle.getString("Data");

    重要方法

    clear():清除此Bundle映射中的所有保存的数据。

    clone():克隆当前Bundle

    containsKey(String key):返回指定key的值

    getString(String key):返回指定key的字符

    hasFileDescriptors():指示是否包含任何捆绑打包文件描述符

    isEmpty():如果这个捆绑映射为空,则返回true

    putString(String key, String value):插入一个给定key的字符串值

    readFromParcel(Parcel parcel):读取这个parcel的内容

    remove(String key):移除指定key的值

    writeToParcel(Parcel parcel, int flags):写入这个parcel的内容

    与SharedPreferences的区别

    SharedPreferences是简单的存储持久化的设置,就像用户每次打开应用程序时的主页,它只是一些简单的键值对来操作。它将数据保存在一个xml文件中

    Bundle是将数据传递到另一个上下文中或保存或回复你自己状态的数据存储方式。它的数据不是持久化状态。

  • 相关阅读:
    显示多行字符串
    dowhile
    获取系统的相关文件夹
    DLL 实际应用版本
    ShellExecute
    LoadLibrary
    MFC DLL的创建
    在DLL中存放资源
    替换字符串
    RemoveDirectoryB
  • 原文地址:https://www.cnblogs.com/feary/p/5382682.html
Copyright © 2011-2022 走看看