即使在今天发现自己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(){
@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);
Bundle bundle = getIntent().getExtras(); //得到传过来的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 bundle存放数据详解

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

  2. 解决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 ...

  3. React Native: unable to load scripts from assets 'index.android.bundle' on real device

    问题:重新建了一个项目后,运行react-native run-android报: unable to load scripts from assets 'index.android.bundle' ...

  4. Unable to load script from assets 'index.android.bundle'.make sure you bundle is packaged correctly

    解决此问题 以下方法每次都需要执行命令2才能更新 1.创建assets目录 mkdir android/app/src/main/assets 2.执行命令 react-native bundle - ...

  5. 项目初始化以后出现:Unable to load script from assets 'index.android.bundle

    Mac中真机测试React Native project时出现Unable to load script from assets 'index.android.bundle' 2018年01月21日 ...

  6. react native中Unable to load script from assets 'index.android.bundle'解决方案

    刚刚朋友问我,说是创建好一个项目,运行后报错:Unable to load script from assets 'index.android.bundle',以前好好的没出现这种现象,于是我找到一个 ...

  7. Unable to load script from assets 'index.android.bundle' 出错?

    野路子太多,坑人真的!F**k 言归正传,当你运行 react native 程序的时候出现这个错误 ,如果您使用Windows,请按以下方式运行命令,或者如果出现错误“无法找到条目文件index.a ...

  8. React-Native 之 index.android.bundle

    问题: index.android.bundle  这个bug 我相信很少同学会遇到,然而就是这个问题,困扰了我跟我的同事多天, 各种方法处理:  进入 android 目录  ./gradlew c ...

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

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

随机推荐

  1. Python逐块读取大文件行数的代码 - 为程序员服务

    Python逐块读取大文件行数的代码 - 为程序员服务 python数文件行数最简单的方法是使用enumerate方法,但是如果文件很大的话,这个方法就有点慢了,我们可以逐块的读取文件的内容,然后按块 ...

  2. struts2+jquery +json实现异步加载数据,亲测(原创)

    //初始加载页面时 $(document).ready(function(){ //为获取单个值的按钮注册鼠标单击事件 $("#getMessage").click(functio ...

  3. Program1-1

    package com.algo; import java.util.Random; import java.util.Scanner; public class P1_1 { static int ...

  4. Vijos P1881 闪烁的星星

    背景 星光闪耀--深蓝色空间 听说过他们的语言 沉默 暮 他们称赞深相互 描写叙述 繁星, 漫天的繁星. 繁星排成一列, 我数一数呀, 一共同拥有N仅仅小星星呢. 星星们是听话的好孩子, 小岛在指挥它 ...

  5. Android 动态生成布局 (多层嵌套)

    Android 除了能够载入xml文件,显示布局外,也能够代码生成布局,并通过setContentView(View view)方法显示布局.单独的一层布局,如一个主布局加一个控件(如Button\i ...

  6. linux在下面APK反编译软件和过程的描述

    需要的工具: 1.apktool apk打包工具 下载地址:http://android-apktool.googlecode.com/files/apktool1.5.2.tar.bz2 安装:直接 ...

  7. VB.NET 机房收费系统项目总结

    VB.NET机房收费系统项目总结 从2013年5月3日——2013年8月20日历时三个多月的.NET机房收费系统终于完成了.项目做完了,真有一种如释重负的感觉. 下面我将从文档.UML图,代码这三个方 ...

  8. 百度地图AP1

    百度地图API的用法 百度地图API演示样例 百度地图API学习总结 <1> <%@ Page Language="C#" Inherits="Syst ...

  9. MyEclipse导入主题文件epf后xml及jsp等页面中点击标签之后显示灰白

    MyEclipse导入主题文件epf后xml及jsp等页面中点击标签之后显示灰白,症状例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVVAxOT ...

  10. ssh 即使主机,同nohup背景脚本

    下面的脚本工具:先从本地副本的脚本到远程主机,然后ssh即使在远程主机,脚本的运行副本前(因为脚本需要运行很长,它运行在后台),该脚本仅用于备忘录,如果请指点不足! #!/bin/bash cd /t ...