Activity之间传递数据的三种方式详解

来源:互联网 发布:服务器如何开放端口 编辑:程序博客网 时间:2024/05/18 01:25

Activity之间传递数据有三种方式

1.通过Bundle传递简单数据
2.通过Serializable方式传递对象
3.通过Parcelable方式传递对象

通过Bundle对象传递数据的代码:

//点击按钮传递数据的方法    public void button1(View v) {        Intent intent=new Intent(this,Activity2.class);        String s=editText1.getText().toString();        //通过Bundle对象传递数据        Bundle bundle=new Bundle();        bundle.putString("info",s);        intent.putExtra("box",bundle);        startActivity(intent);    }

通过Bundle对象接收数据的代码:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity2);        textView1=(TextView) findViewById(R.id.textView1);        //获取Intent        Intent intent =getIntent();        //通过Intent获取bundle对象        Bundle bundle=intent.getBundleExtra("box");        //取出数据        String s=bundle.getString("info");        textView1.setText(s);        //最后给textView设置进去并显示出来    }

Bundle传递数据小结:

你可以把Intent理解为一个快递员
把Bundle理解为一个箱子

//传递数据的理解:
你把数据写好装到了Bundle这个箱子里面去
把这个箱子交给快递员Intent

//接收数据的理解:
getIntent这个Intent快递员接收到了那个快递员发送来的Bundle箱子
你把这个箱子打开取出数据就可以了


二、通过Serializable方式传递对象

发送一个Cat对象:

//点击按钮传递一个对象的方法    public void button2(View v) {            Cat Cat=new Cat();            Cat.name="小花猫";            Cat.age=2;            Cat.sex="母";            Intent intent=new Intent(this,Activity3.class);            //注意!Intetn.putExtra是没有put一个Object对象的方法的            //put的对象必须要实现  Serializable接口,不然eclipse会报错            intent.putExtra("people",people);            startActivity(intent);        }

接收一个Cat对象:

public class Activity3 extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity3);        //开始获取Cat对象        Intent intent =getIntent();        //注意你要通过getSerializableExtra()方法来获取一个对象        Cat Cat=(Cat) intent.getSerializableExtra("Cat");        //最后给textView设置进去并显示出来        Toast.makeText(this,Cat.toString() ,Toast.LENGTH_LONG).show();;    }}

Serializable传递自定义对象数据小结:

1.发送一个对象,此对象要实现Serializable这个序列化接口
2.接收对象使用intent.getSerializableExtra();方法进行接收


三、Activity通过Parcelable方式传递自定义对象数据

注意看本段内容之前请看这篇关于Parcelable()详细介绍:
传送门:http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

传递一个people对象

//点击按钮使用Parcelable(包裹)的方式传递一个对象的方法        public void button3(View v) {            people people=new people();                people.name="小明";                people.age=15;                people.sex="男";                Intent intent=new Intent(this,Activity4.class);                //注意!Intetn.putExtra是没有put一个Object对象的方法的                //put的对象必须要实现  Serializable接口或者Parcelable接口,不然eclipse会报错                intent.putExtra("people",people);                startActivity(intent);            }

接收一个people对象

package com.example.chuandidata;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.provider.Contacts.People;import android.widget.Toast;public class Activity4 extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity4);        //开始获取使用Parcelable的方式传进过来的对象        Intent intent =getIntent();        people p=intent.getParcelableExtra("people");        Toast.makeText(this,p.toString() ,Toast.LENGTH_LONG).show();;    }}

Serializable传递自定义对象数据小结:

1.发送一个对象,此对象要实现Parcelable这个序列化接口2.接收对象使用intent.getParcelableExtra();方法进行接收

尾声:

如果你要使用实现ParcelableActivity之间传递数据的话 , 你就要在实现Parcelable的类中*实例化静态内部对象CREATOR实现接口Parcelable.Creator 可以理解这个一个反序列化的过程

我给你你提供了一个:

实例化静态内部对象CREATOR实现接口Parcelable.Creator模板:

package com.example.chuandidata;import android.os.Parcel;import android.os.Parcelable;public class people implements Parcelable {    public String name;    public String sex;    public int age;     /*      * 实例化静态内部对象CREATOR实现接口Parcelable.Creator      * 可以理解这个一个反序列化的过程      */    public static final Parcelable.Creator<people> CREATOR = new Parcelable.Creator<people>(){        //方法1       public people createFromParcel(Parcel in){           people people=new people();           //注意读的关系要和上面定义的全部变量一一对应           people.name=in.readString();           people.sex=in.readString();           people.age=in.readInt();           return people;        }       /*        * 方法2        * 用于供外部进行反序列化的people[]数组        * 返回一个people[]数组;        */        public people[] newArray(int size){            return new people[size];        }    };    /*     * 重写Parcelable的方法     * 这个是一个描述方法  我们直接返回0就行了     */    @Override    public int describeContents() {        // TODO Auto-generated method stub        return 0;    }    /*     * 重写Parcelable的方法     * 参数1(Parcel包裹,flags标记)     * 这个方法是把你要传递的数据写到包裹里面去     */    @Override    public void writeToParcel(Parcel dest, int flags) {        // TODO Auto-generated method stub        //把要写的值都write进去;        dest.writeString(name);        dest.writeString(sex);        dest.writeInt(age);    }    @Override    public String toString() {        return "people [name=" + name + ", sex=" + sex + ", age=" + age + "]";    }}

完!