Android Send Object via Intent

The scenario is, I had 1 POJO class and I have to send the instance object to another activity. Before perform this task, we need to modify the POJO class to make it shareable within android activity.

Below is my POJO class.

public class Person {
    private String name;
    private long age;
    private String email;

    // getter & setter goes here.
}

From POJO class above, we have to implement Parceable interface, and for sure you have to implement 2 methods “describeContents” and “writeToParcel”.

Here is “writeToParcel” method implementation and just ignore “describeContents” method.

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeLong(age);
    dest.writeString(email);
}

Add 2 constructors in our existing POJO class.

// For normal object creation.
public Person(){}

public Person(Parcel source) {
    name = source.readString();
    age = source.readLong();
    email = source.readString();
}

Last step is create CREATOR for our POJO Parcelable class.

public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
    @Override
    public Person createFromParcel(Parcel source) {
        return new Person(source);
    }

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

Below is my complete POJO class.

public class Person implements Parcelable {

    private String name;
    private long age;
    private String email;

    // For normal object creation.
    public Person(){}

    public Person(Parcel source) {
        // Bear in mind that these order must be the same as
        // with writen order in `writeToParcel` method and also
        // aware with the data type. 
        name = source.readString();
        age = source.readLong();
        email = source.readString();
    }

    // getter & setter goes here.

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeLong(age);
        dest.writeString(email);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }

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

Now you can send your instance object to another activity with intent, like code below.

// MyActivity.java
Person p = new Person();
p.setName("John Doe");
p.setAge(45L);
p.setEmail("john.doe@email.com");

Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
intent.putExtra("uniqueKey", p);
startActivity(intent);

// Receiving object from MyActivity.java
Intent intent = getIntent();
Person person = intent.getParcelableExtra("uniqueKey");

Reference : http://stackoverflow.com/a/2141166

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s