安卓进阶篇-Bundle
来源:互联网 发布:openwrt 修改mac地址 编辑:程序博客网 时间:2024/04/29 22:29
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.”
,它类似于Map,用于存放key-value名值对形式的值。相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap类型的变量来存放putXxx()方法放入的值:public
public
}
}
类继承关系:
java.lang.Object
android.os.Bundle
Bundle类是一个final类:
public final class
Bundle
extends Objectimplements Parcelable Cloneable
在调用Bundle对象的getXxx()方法时,方法内部会从该变量中获取数据,然后对数据进行类型转换,转换成什么类型由方法的Xxx决定,getXxx()方法会把转换后的值返回。
一、API文档说明
1.介绍
用于不同Activity之间的数据传递
1.重要方法
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的内容
两个activity之间的通讯可以通过bundle类来实现,做法就是:
1)新建一个bundle类
- Bundle mBundle = new Bundle();
- mBundle.putString("Data", "data from TestBundle");
(3)新建一个intent对象,并将该bundle加入这个intent对象
- Intent intent = new Intent();
- intent.setClass(TestBundle.this, Target.class);
- intent.putExtras(mBundle);
代码实例:
例子1:
两个类如下:intent从TestBundle类发起,到Target类。
类1:TestBundle类
- Intent intent = new Intent();
- intent.setClass(TestBundle.this, Target.class);
- Bundle mBundle = new Bundle();
- mBundle.putString("Data", "data from TestBundle");//压入数据
- intent.putExtras(mBundle);
- startActivity(intent);
- Bundle bundle = getIntent().getExtras(); //得到传过来的bundle
- String data = bundle.getString("Data");//读出数据
- setTitle(data);
使用Bundle在Activity间传递数据
从源Activity 中传递数据
//数据写入Intent
Intent openWelcomeActivityInten
Bundle myBundelForName=new Bundle();
myBundelForName.putString("Key_Name",inName.getText().toString());
myBundelForName.putString("Key_Age",inAge.getText().toString());
openWelcomeActivityInten
openWelcomeActivityInten
startActivity(openWelcomeActivityInten
目标Activity 中获取数据
//从Intent 中获取数据
Bundle myBundelForGetName=this.getIntent().getExtras();
String name=myBundelForGetName.getString("Key_Name");
myTextView_showName.setText("欢迎您进入:"+name);
使用Bundle在Activity间传递数据2
private Intent toNextIntent;//Intent 成员声明
toNextIntent=new Intent();//Intent 定义
toNextIntent.setClass(TwoActivityME3.this, SecondActivity3.class);
//设定开启的下一个Activity
startActivityForResult(toNextIntent, REQUEST_ASK);
//开启Intent 时候,把请求码同时传递
在源请求Activity 中等待Intent 返回应答结果,通过重载onActivityResult()方法
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_ASK){
if(resultCode==RESULT_CANCELED){
setTitle("Cancel****");
}else if(resultCode==RESULT_OK){
showBundle=data.getExtras();//从返回的Intent中获得Bundle
Name=showBundle.getString("myName");//从bundle中获得相应数据
text.setText("the name get from the second layout:\n"+Name);
}
}
}
? 第一个参数是你开启请求Intent时的对应请求码,可以自己定义。
? 第二个参数是目标Activity返回的验证结果码
? 第三个参数是目标Activity返回的Intent
目标Activity 中发送请求结果代码,连同源Activity 请求的数据一同绑定到Bundle
中通过Intent 传回源请求Activity 中
Log与DDMS(查看Log等信息)
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_ASK){
if(resultCode==RESULT_CANCELED){
setTitle("Cancel****");
}else if(resultCode==RESULT_OK){
showBundle=data.getExtras();//从返回的Intent中获得Bundle
Name=showBundle.getString("myName");//从bundle中获得相应数据
text.setText("the name get from the second layout:\n"+Name);
}
}
}
目标Activity 中发送请求结果代码,连同源Activity 请求的数据一同绑定到Bundle
中通过Intent 传回源请求Activity 中
stringBundle=new Bundle();
stringBundle.putString("myName", Name);
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);//返回Activity结果码
finish();
另外:与SharedPreferences的区别
SharedPreferences是简单的存储持久化的设置,就像用户每次打开应用程序时的主页,它只是一些简单的键值对来操作。它将数据保存在一个xml文件中
Bundle是将数据传递到另一个上下文中或保存或回复你自己状态的数据存储方式。它的数据不是持久化状态。
- 安卓进阶篇-Bundle
- 安卓学习140502:Bundle
- 安卓开发bundle传值
- 安卓进阶专题篇-Intent
- android安卓技能进阶篇
- 安卓学习进阶
- 安卓jni进阶
- 安卓 ADT-bundle 官方地址
- 安卓代码段--传递Bundle数据
- 安卓高级工程师的进阶
- 安卓自定义View进阶
- 安卓自定义View进阶
- 安卓自定义 View 进阶:Path 完结篇(伪)
- 安卓自定义View进阶-Path之完结篇
- 安卓自定义 View 进阶:Path 完结篇
- 安卓开发-ContentValues类+Bundle类的作用
- 安卓开发41:状态保存 Bundle savedInstanceState
- 安卓开发环境ADT Bundle for Windows
- iOS 7 新特性:视图控制器切换API
- SQLALchemy backref章节文档
- 死锁
- Flask学习记录之Flask-Admin
- ArcGIS教程:Arcmap栅格数据格式转换
- 安卓进阶篇-Bundle
- GIT学习笔记
- 创建一个自己的GitHub,创建自己的开源项目
- 可以在html中使用的特殊字符-ISO Latin-1字符集---换行等操作符
- 在使用flask-script的应用上使用gunicorn
- codeforces 538B
- C++最好的释放对象内存
- 驱动模块Makefile 分析
- Triangle