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类
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value) (3)新建一个intent对象,并将该bundle加入这个intent对象 两个类如下:intent从TestBundle类发起,到Target类。
类1:TestBundle类:
类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);
- }
- }
概况为以下几步:
1、实例化Bundle 一个对象,用putString(标记,数据)来将数据导入到Bundle对象中;
2、然后将Bundle对象导入到Intent对象中;
3、Intent启动另一个activity。
4、Bundle对象可以从activity.getIntent().getExtras()中返回。可见,启动当前activity
的Intent对象是由getIntent()来找到的。
5、通过Bundle的getString()方法,就可以读出所要的数据。