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类是一个final类:
public final class
Bundle
extends Objectimplements Parcelable Cloneable
两个activity之间的通讯能够通过bundle类来实现,做法就是:
(1)新建一个bundle类
Bundle mBundle = new Bundle();
(2)bundle类中增加数据(key -value的形式,还有一个activity里面取数据的时候,就要用到key,找出相应的value)
mBundle.putString("Data", "data from TestBundle");
(3)新建一个intent对象,并将该bundle增加这个intent对象
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
intent.putExtras(mBundle);
完整代码例如以下:
android mainfest.xml例如以下:
<?xml version="1.0" encoding="utf-8"?
> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tencent.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TestBundle" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Target"></activity> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
两个类例如以下:intent从TestBundle类发起,到Target类。
类1:TestBundle类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class TestBundle extends Activity { private Button button1;private OnClickListener cl; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);button1 = (Button) findViewById(R.id.button1);cl = new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent(); intent.setClass(TestBundle.this, Target.class); Bundle mBundle = new Bundle(); mBundle.putString("Data", "data from TestBundle");//压入数据 intent.putExtras(mBundle); startActivity(intent);}};button1.setOnClickListener(cl);}
}
类2: Target
import android.app.Activity;
import android.os.Bundle; public class Target extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.target); Bundle bundle = getIntent().getExtras(); //得到传过来的bundleString data = bundle.getString("Data");//读出数据 setTitle(data); }
}
布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button"android:id = "@+id/button1"/>
</LinearLayout>
target.xml
<?xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/target" /> </LinearLayout>
String.xml
<?
xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, TestBundle!</string> <string name="app_name">測试Bundle使用方法</string> <string name="button">点击跳转</string> <string name="target">来到target activity</string> </resources>
结果:
跳转结果:
Android Bundle类别相关推荐
- Android Bundle类 学习总结
Android Bundle类 http://blog.csdn.net/randyjiawenjie/article/details/6651437 根据google官方的文档(http://dev ...
- Unable to load script from assets ‘index.android.bundle‘.
打开React-Native项目,运行之后出现标题错误.百度之后的文章都是如下: 1.首先手动在android/app/src/main目录下建立一个assets文件夹2.然后在项目的根目录下执行 r ...
- React Native之提示Unable to load script from assets ‘index.android.bundle
1 问题 Unable to load script from assets 'index.android.bundle'. Make sure your bundle is packaged cor ...
- 解决React Native unable to load script from assets index.android.bundle on windows
React Native运行的时候,经常碰到React Native unable to load script from assets index.android.bundle on windows ...
- 无法从Windows上的资产index.android.bundle加载脚本
本文翻译自:Unable to load script from assets index.android.bundle on windows I'm trying to run my first R ...
- android bundle 对象,Android Bundle传递对象
首先Android的Bundle是可以传递对象的.我们可以用Bundle b = new Bundle():b.putSerializable("key", 对象引用); 但是这样 ...
- Android Bundle 实例介绍
文章目录 ####1.Bundle简介 Bundle 主要用于传递数据,它保存的数据,是以Key-Value (键值对)的形式存在的. 1)我们经常使用的Bundle在Activity之间传递数据,传 ...
- react native unable to load script from sets “index.android.bundle“.make sure your bundle is packag
unable to load script from sets "index.android.bundle".make sure your bundle is packaged c ...
- Android: Bundle机制
Bundle在Android开发中十分常见,常用于以下场合: 1.Activity状态数据的保存与恢复涉及到的两个回调:void onSaveInstanceState (Bundle outStat ...
最新文章
- 知乎算法团队负责人孙付伟:Graph Embedding在知乎的应用实践
- 全球与中国丙烷脱氢制丙烯市场发展形势与前景规划分析报告2022-2028年版
- 怎么在大学当院系负责人呢?一个case study
- python编写装饰器_我也来写一下python装饰器
- 代码补全_补全股票代码位数的一百种姿势
- 互联网络层的内核实现[内核中的路由机制]
- 线性回归(单神经元,多神经元)和多层感知机(多个神经元)对比
- 史上最全的微服务知识科普
- Nginx的端口修改问题
- Script error.全面解析
- Azure 基础:Blob Storage
- 概率论——离散型随机变量
- java正态分布随机数产生方法
- 【天下有春】七日之书
- 2018-08-08 Mac使用中的一些实用设置
- CTO问我,为什么需要API网关?
- TSC打印机打印条形码和二维码,applet实现方式
- 谷歌地图实现车辆位置实时跟踪
- 微信公众号分享给微信好友和朋友圈
- 用construct2做一个酷炫到爆炸的海绵宝宝大战痞老板游戏
热门文章
- win32_bios 的对象编辑器无法保存对象_怎样创建Femap对象
- mysql8.0.11启动不了_8.0.11版本Mysql遇到MySQL 服务无法启动的解决方法
- oracle Blob保存方式,关于文件保存到Oracle中BLOB字段的方法及例子
- 安装mysql5 1步骤_Linux系统安装MySQL详细步骤(mysql-5.1等)
- html文本域 高度自适应,textarea高度自适应,textarea随着内容高度变化
- android手势监听功能吗,Android手势监听 OnGestureListener (短按,长按,慢滑动,快滑动)...
- 占用系统资源测试_高频性能测试面试题15道
- 计算机维修知识综述论文,机器学习领域各领域必读经典综述论文整理分享
- Java类名.方法和变量
- C语言从来都没有过时,你大爷终究是你大爷