今天发现自己连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类
-
BundlemBundle=newBundle();
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
-
mBundle.putString("Data","datafromTestBundle");
(3)新建一个intent对象,并将该bundle加入这个intent对象
-
Intentintent=newIntent();
-
intent.setClass(TestBundle.this,Target.class);
-
intent.putExtras(mBundle);
完整代码如下:
android mainfest.xml如下:
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.tencent.test"
-
android:versionCode="1"
-
android:versionName="1.0">
-
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
-
<activityandroid:name=".TestBundle"
-
android:label="@string/app_name">
-
<intent-filter>
-
<actionandroid:name="android.intent.action.MAIN"/>
-
<categoryandroid:name="android.intent.category.LAUNCHER"/>
-
</intent-filter>
-
</activity>
-
<activityandroid:name=".Target"></activity>
-
</application>
-
<uses-sdkandroid:minSdkVersion="7"/>
-
</manifest>
两个类如下:intent从TestBundle类发起,到Target类。
类1:TestBundle类:
-
importandroid.app.Activity;
-
importandroid.content.Intent;
-
importandroid.os.Bundle;
-
importandroid.view.View;
-
importandroid.view.View.OnClickListener;
-
importandroid.widget.Button;
-
-
publicclassTestBundleextendsActivity{
-
-
privateButtonbutton1;
-
privateOnClickListenercl;
-
publicvoidonCreate(BundlesavedInstanceState){
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
button1=(Button)findViewById(R.id.button1);
-
cl=newOnClickListener(){
-
@Override
-
publicvoidonClick(Viewarg0){
-
-
Intentintent=newIntent();
-
intent.setClass(TestBundle.this,Target.class);
-
BundlemBundle=newBundle();
-
mBundle.putString("Data","datafromTestBundle");
-
intent.putExtras(mBundle);
-
startActivity(intent);
-
}
-
};
-
button1.setOnClickListener(cl);
-
}
-
}
类2: Target
-
importandroid.app.Activity;
-
importandroid.os.Bundle;
-
-
publicclassTargetextendsActivity{
-
-
publicvoidonCreate(BundlesavedInstanceState){
-
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.target);
-
<spanstyle="color:#ff6600;">Bundlebundle=getIntent().getExtras();</span>
-
Stringdata=bundle.getString("Data");
-
setTitle(data);
-
-
}
-
}
布局文件:
main.xml
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns: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
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns: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
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<resources>
-
<stringname="hello">HelloWorld,TestBundle!</string>
-
<stringname="app_name">测试Bundle用法</string>
-
<stringname="button">点击跳转</string>
-
<stringname="target">来到targetactivity</string>
-
</resources>
结果:
跳转结果:
今天发现自己连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类
-
BundlemBundle=newBundle();
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
-
mBundle.putString("Data","datafromTestBundle");
(3)新建一个intent对象,并将该bundle加入这个intent对象
-
Intentintent=newIntent();
-
intent.setClass(TestBundle.this,Target.class);
-
intent.putExtras(mBundle);
完整代码如下:
android mainfest.xml如下:
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.tencent.test"
-
android:versionCode="1"
-
android:versionName="1.0">
-
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
-
<activityandroid:name=".TestBundle"
-
android:label="@string/app_name">
-
<intent-filter>
-
<actionandroid:name="android.intent.action.MAIN"/>
-
<categoryandroid:name="android.intent.category.LAUNCHER"/>
-
</intent-filter>
-
</activity>
-
<activityandroid:name=".Target"></activity>
-
</application>
-
<uses-sdkandroid:minSdkVersion="7"/>
-
</manifest>
两个类如下:intent从TestBundle类发起,到Target类。
类1:TestBundle类:
-
importandroid.app.Activity;
-
importandroid.content.Intent;
-
importandroid.os.Bundle;
-
importandroid.view.View;
-
importandroid.view.View.OnClickListener;
-
importandroid.widget.Button;
-
-
publicclassTestBundleextendsActivity{
-
-
privateButtonbutton1;
-
privateOnClickListenercl;
-
publicvoidonCreate(BundlesavedInstanceState){
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
button1=(Button)findViewById(R.id.button1);
-
cl=newOnClickListener(){
-
@Override
-
publicvoidonClick(Viewarg0){
-
-
Intentintent=newIntent();
-
intent.setClass(TestBundle.this,Target.class);
-
BundlemBundle=newBundle();
-
mBundle.putString("Data","datafromTestBundle");
-
intent.putExtras(mBundle);
-
startActivity(intent);
-
}
-
};
-
button1.setOnClickListener(cl);
-
}
-
}
类2: Target
-
importandroid.app.Activity;
-
importandroid.os.Bundle;
-
-
publicclassTargetextendsActivity{
-
-
publicvoidonCreate(BundlesavedInstanceState){
-
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.target);
-
<spanstyle="color:#ff6600;">Bundlebundle=getIntent().getExtras();</span>
-
Stringdata=bundle.getString("Data");
-
setTitle(data);
-
-
}
-
}
布局文件:
main.xml
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns: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
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns: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
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<resources>
-
<stringname="hello">HelloWorld,TestBundle!</string>
-
<stringname="app_name">测试Bundle用法</string>
-
<stringname="button">点击跳转</string>
-
<stringname="target">来到targetactivity</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