根据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(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
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);
}
};
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);
<span style="color:#ff6600;">Bundle bundle = getIntent().getExtras(); </span> //得到传过来的bundle
String 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类的更多相关文章

  1. Android——Android Bundle类(转)

    今天发现自己连Bundle类都没有搞清楚,于是花时间研究了一下. 根据google官方的文档(http://developer.android.com/reference/android/os/Bun ...

  2. Android之Bundle类

    API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsKey ...

  3. android 18 Bundle类

    Bundle类:竖屏的activity换到横屏的activity的时候,会把竖屏的activity杀掉横屏的activity创建,竖屏的activity会有一些计算结果,可以用数据存起来,存到内存里面 ...

  4. Android笔记——Bundle类的作用

    Bundle类用作携带数据,它类似于Map,用于存放key-value键值对形式的值.相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getSt ...

  5. android bundle存放数据详解

    转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...

  6. Android Bundle类别

    即使在今天发现自己Bundle类不明确,因此,花时间去研究了一下. 依据google官方文件(http://developer.android.com/reference/android/os/Bun ...

  7. Android Bundle详解

    http://blog.csdn.net/cswhale/article/details/39053411 1 Bundle介绍 Bundle主要用于传递数据:它保存的数据,是以key-value(键 ...

  8. Android——Android Bundle详解(转)

    Android Bundle详解 1 Bundle介绍 Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. 我们经常使用Bundle在Activity之间传递数 ...

  9. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

随机推荐

  1. Ubuntu+Nginx+PHP的最简搭建方法

    先安装: sudo apt-get install nginx php5-fpm -y 然后编辑配置文件: /etc/nginx/site-available/default 找到"loca ...

  2. Keil的lib生成个人总结

    1. 生成lib 的工程可以没有main函数,可以只有一个.c文件,一个.c文件中可以只有一个函数 2. 需要在lib工程中建立一个.h文件, 必须用extern声明各全局变量和函数. 3. 调用li ...

  3. C#winform中ListView的使用

    使用ListView模仿Windows系统的资源管理器界面,实现文件(夹)的浏览.重命名.删除及查询等功能,主要功能界面展示如下: 1.MainForm.cs及MainForm.Designer.cs ...

  4. Visual Studio解决方案及项目的配置

    配置解决方案的属性 1.配置解决方案平台,该配置实际上修改的是解决方案目录下的sln(solution)文件. 配置项目的属性 1.配置项目平台及项目的目标平台:项目-右键-属性-生成(竖着第二个选项 ...

  5. [教程] 以本论坛为例,手把手教你使用按键精灵POST登陆网页

    本帖最后由 isaacc 于 2012-2-26 11:08 编辑 整个操作,很无脑.只要你够勤快,你学不会,你来咬我.懒人和伸手党就直接复制代码去玩吧,但我不是叫你拿去干坏事. 准备工具:WPE和I ...

  6. epoll 实际使用

    void DataHandle::recv() {     sleep(2);     _data_router -> readInfoHw(&mInfo);     ALOGD(SYS ...

  7. java ReentrantReadWriteLock

    // read and write lock is mutual exclusion lock //Listing 7-3. Using ReadWriteLock to Satisfy a Dict ...

  8. Ubuntu Nginx下配置网站ssl实现https访问

    最近在看  HTTP权威指南   看到介绍了HTTPS的ssl,自己就动手测试了下,将步骤记录下 HTTPS简介 什么是HTTPS?百科是这样解释的.HTTPS(全称:Hyper Text Trans ...

  9. IOS应用程序生命周期的AppDelegate详解

    IOS 中的 AppDelegate.m/h 文件是很重要的呢,因为它是对 Application 的整个生命周期进行管理的. 先明白,每个iPhone应用程序都有一个UIApplication,UI ...

  10. android Textview动态设置大小

    import android.app.Activity; //import com.travelzen.tdx.BaseActivity; //import com.travelzen.tdx.uti ...