Bundle, 传递数据的包裹
来源:互联网 发布:html5翻牌小游戏源码 编辑:程序博客网 时间:2024/04/30 23:57
自己用过一段时间的bundle,但是每次都是写写就完事了,并不是很了解bundle,今天就自己好好看看。
在Activity之间的通信过程中,intent可以携带数据,每次携带一个键值对,无法一次性带很多,而bundle提供了一个好的打包工具,可以让intent这个信使一下可以携带多条键值对,我们先来看看bundle的类。
public final class Bundle
extends Objectimplements Parcelable, Cloneable
A mapping from String values to various Parcelable types.- 它是一个用来存储键值对的,有四种构造函数
Bundle()
Constructs a new, empty Bundle.Bundle(Bundle b)
Constructs a Bundle containing a copy of the mappings from the given Bundle.Bundle(ClassLoader loader)
Constructs a new, empty Bundle that uses a specific ClassLoader for instantiating Parcelable and Serializable objects.Bundle(int capacity)
Constructs a new, empty Bundle sized to hold the given number of elements.
- Bundle的方法有很多,但是主要集中在两个大的方面,就是存取数据,存,以put 开头,可以存入字符,字节,有很多,与之相反,取以get开头,获得相应的value。
在与intent的配合使用中,首先,建立信使,创建intent,putExtras(Bundle bundle) 向intent中放入需要的“携带”的数据,并且在数据包中用put开头的函数存入自己想存入的数据。 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this,BundleSecActivity.class); Bundle bundle = new Bundle(); bundle.putString("key", "这是发送的bundle信息!!!"); intent.putExtras(bundle); startActivity(intent); }
在另外的activity,获取信使,采用getExtras()方法获得bundle数据包,再从里面获得数据。public class BundleSecActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bundle_sec); TextView showInfo = (TextView) findViewById(R.id.showInfo); String show = getIntent().getExtras().getString("key"); showInfo.setText(show); }}
0 0
- Bundle, 传递数据的包裹
- Android开发中Bundle用法 包裹数据
- Android开发中Bundle用法包裹数据
- <Bundle>通过bundle进行Activity之间的数据传递
- bundle实现Activity之间的数据传递
- 我对Bundle传递数据的理解
- 使用Intent和Bundle的数据传递
- Android activity 之间的数据传递 bundle
- Intent传递数据和Bundle传递数据的区别
- Android 数据传递Bundle
- Android Bundle数据传递
- Bundle传递数据
- 数据传递intent,Bundle
- Bundle数据传递案例
- 【数据】不同Activity之间的数据传递——Bundle
- 如何使用 Bundle 传递数据?
- 使用Bundle传递复杂数据
- Android Intent传递Bundle数据
- Java 数组类型(基本类型数组类型、封装类型数组类型、数组的数组类型)
- RDDAPI详解rdd数据模型及rdd的sql实现
- 18. Window createPopup() 方法
- 2015年多校联合训练第三场RGCDQ(hdu5317)
- 二叉树基本操作
- Bundle, 传递数据的包裹
- redis持久化RDB和AOF
- C# Chart 点击获取当前点击坐标和Series
- OC学习:OC中的可变字典NSMutableDictionary和字典的遍历
- 逻辑地址、线性地址、物理地址
- hdu 4901——The Romantic Hero
- python 函数的参数
- 19. Window focus() 方法
- 【网络安全】Telnet 23端口扫描与信息提取