Bundle自定义数据传递

      我们都知道Bundle可能过put****()方法添加各种基本类型的数据,Intent也可以通过putExtras(Bundle)将数据添加进去,然后通过startActivity()跳到下一下Activity的时候就把数据也传到下一个Activity了。如传递一个字符串到下一个Activity

把数据放到Intent里

 btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
           public void onClick(View v) {

                Intent intent1=new Intent(MyActivity.this,SecondActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("title","Activity 2");//把Activity 2传给下一个Activity
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
 });

接收端Activity代码为:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);
        //获取bundle数据
        Bundle bundle=getIntent().getExtras();
        String text=bundle.getString("title");//根据key来获取
        TextView title=(TextView)findViewById(R.id.title);
        title.setText(text+"   "+person.getName()+"  "+person.getAge());
    }

上面传递的是基本数据类型的数据,现在主要来说明自定义数据类型的传递。

1。序列化(详情参看java的序列化

定义Person类实现Serializable接口,Serializable接口没有定义任何方法,是一个标记接口

package com.example.AndroidStudy;

import java.io.Serializable;

public class Person  implements Serializable{

    private String name;

    private int age;//可用transient修饰不序列化某属性

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

使用 Bundle.putSerializable()方法把自定义Person数据放入Bundle内

 Intent intent1=new Intent(MyActivity.this,SecondActivity.class);
 Bundle bundle=new Bundle();
 bundle.putString("title","Activity 2");
 bundle.putSerializable("person",new Person("小白",20));
 intent1.putExtras(bundle);
 startActivity(intent1);

使用Bundle.getSerializable()来获取自定义类型数据

 //获取bundle数据
 Bundle bundle=getIntent().getExtras();
 String text=bundle.getString("title");//根据key来获取
 Person person=(Person)bundle.getSerializable("person");

2.实现Parcelable接口

     Parcelable是Android特有的功能,效率要比实现Serializable接口高。但实现Parcelable稍微复杂一些,推荐使用这种方法。

1.自定义Person类实现 Parcelable接口

package com.example.AndroidStudy;

import android.os.Parcel;
import android.os.Parcelable;

public class Person  implements Parcelable{

    private String name;

    private   int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //重写下面两个方法
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //把数据写入Parcel
        dest.writeString(name);
        dest.writeInt(age);
    }

    //还必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法

    public static final  Creator<Person>CREATOR=new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel source) {
            //读取时返回Person对象---根据Parcel写入的数据生成Person返回
            return new Person(source.readString(), source.readInt());
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}

 使用 Bundle.putParcelable()方法把自定义Person数据放入Bundle内

Intent intent1=new Intent(MyActivity.this,SecondActivity.class);
Bundle bundle=new Bundle();
bundle.putString("title","Activity 2");
bundle.putParcelable("person",new Person("小白",20));
intent1.putExtras(bundle);
startActivity(intent1);

使用Bundle.getParcelable()来获取自定义类型数据

 //获取bundle数据
 Bundle bundle=getIntent().getExtras();
 String text=bundle.getString("title");//根据key来获取
 Person person=(Person)bundle.getParcelable("person");

猜你喜欢

转载自longpo.iteye.com/blog/2218349