码迷,mamicode.com
首页 > 其他好文 > 详细

Intent传递对象(序列化)

时间:2016-05-02 18:29:03      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

Parcelable

public class Person implements Parcelable {
    private int age;
    private String name;
    

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + "]";
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    public static final Parcelable.Creator<Person> CREATOR=new Creator<Person>() {
        
        @Override
        public Person[] newArray(int size) {
            // TODO Auto-generated method stub
            return new Person[size];
        }
        
        @Override
        public Person createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            Person person =new Person();
            person.name=source.readString();
            person.age=source.readInt();
            return null;
        }
    };
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

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

    public Person() {
        super();
    }
    
}

Intent intent=new Intent(MainActivity.this,NextActivity.class);

Person person=new Person();

person.setName("zhang");

person.setAge(27);

intent.putExtra("person_data",person);

获取:

Person person=(Person)getIntent().getParcelableExtra("person_data");

 

Intent传递对象(序列化)

标签:

原文地址:http://www.cnblogs.com/oldcownotGiveup/p/5452624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!