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);
完整代码如下:
<?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 通过Intent使用Bundle传递对象 Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。 被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io....
Android中用来在Activity之间传递数据可以用Bundle,这是一个在两个Bundle间用Bundle传递数据和接收数据的小例子
Bundle是将数据传递到另一个上下文中或保存或回复你自己状态的数据存储方式
androidbundle
android activity bundle 通信示例
android bundle和activity之间交换数据
Android学习笔记之bundle用法源码,非常非常简单,看了就懂,看了就能掌握bundle的基础用法,以及如何通过Intent传递及取出bundle
Android用Bundle传递数据并返回结果 ,交字符串转为大写字母
安卓Android源码——Bundle.rar
很好的源码,值得学习!
最新android studio 2015/10/12日更新 2015/10/12 android-studio-bundle-141.2288178-windows
Android应用源码之Bundle.zip项目安卓应用源码下载Android应用源码之Bundle.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
一个Android Bundle类的操作实例,new一个Bundle对象,将Bundle对象assign给Intent,调用Activity2,并将要传递的数据传入,加载activity2.xml,得Intent中的Bundle对象,取得Bundle对象中的数据,以findViewById()取得...
一个Android工具类用于将对象保存在一个Bundle中没有任何样板代码
Android 基于Bundle对象编写实现一个标准的体重测量仪,其实就是一个Activity跳转页面然后接收值做处理的例子,代码利用网友写的android可视化GUI布局拖拉工具DroidDraw布局了界面。 有一点需要大家注意的是,...
Intent_Bundle_传参数
Android 数据传递(Intent、Bundle、Serializable、Parcelable等),如项目包有问题请联系我谢谢
android-studio-bundle-145.3537739-windows(19/22)
代码示例说明Android页面跳转,并且叙述了Activity和Intent的用法原理, 透视分析Bundle类的使用介绍
Android应用源码之Bundle_Bundle