How to pass the object one activity to another ?

1.Create a class in separated file  that implements Parcelable interface , which provides two override methods


@Override
     public int describeContents()
    {
      // TODO Auto-generated method stub
return 0;
}
@Override
    public void writeToParcel(Parcel dest, int flags) </em>
    {
        //This method to wrap the parcel here....</em>
}

2. And one more method, we have to implement to read/get the values from parcel when we are calling the getParcelable().This must be pulic.

//Here T refers our future parcelable class,In our example Employee is class name
 public static Parcelable.Creator CREATOR = new Creator() {

@Override
 public T[] newArray(int size) {

T[] st = new T[size];
 return st;
 }

@Override
 public T createFromParcel(Parcel source) {

return new T(source);
 }
 };

Note:Here The second method of the CREATOR class needs to read the values.

3. Important note is that when you are going to write the values in parcelable class, we maintain proper order means that in which way  we insert the data and will get the values in same manner. For example take a employee class, everyone has name and designation, then we are writing the data as follows.

dest.writeString(Name);
dest.writeString(Designation);

For getting the values as follows:

/**
* The constructor is used to get the values in another class.
*/
public Employee(Parcel source)
{
Name = source.readString();
Designation = source.readString();
}

Here we write name as first and second as  designation, we get the result also first name and second designation.Just try with changing order and see the result. Because source.readString() returns which string has written first in Employee class .
Designation = source.readString();
Name = source.readString();

4.In First Activity, we pass the class object with values.

Employee s = new Employee();
s.setName(“James”);
s.setDesignation(“Android Developer”);
Bundle b = new Bundle();
b.putParcelable(“test”, s);

Intent i = new Intent(this, SecondActivity.class);
i.putExtras(b);
startActivity(i);

5.In Second Activity we get the values as follows.
Bundle b = getIntent().getExtras();
Employee emp = b.getParcelable(“test”);
Toast.makeText(this,emp.getName() + “has  desingation :” + emp.getDesignation(), 0)
.show();

The Whole Employee code pasted here .

public class Employee implements Parcelable {

    private String Name, Designation;

    public static Parcelable.Creator CREATOR = new Creator() {

        @Override
        public Employee[] newArray(int size) {

            Employee[] st = new Employee[size];

            return st;
        }

        @Override
        public Employee createFromParcel(Parcel source) {

            return new Employee(source);
        }
    };

    public Employee(Parcel source) {
        Name = source.readString();
        Designation = source.readString();

    }

    public Employee() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(Name);
        dest.writeString(Designation);
    }

    /**
     *@return the name
     */
    public String getName() {
        return Name;
    }

    /**
     *@param name
     *            the name to set
     */
    public void setName(String name) {
        Name = name;
    }

    /**
     *@return the designation
     */
    public String getDesignation() {
        return Designation;
    }

    /**
     *@param designation
     *            the designation to set
     */
    public void setDesignation(String designation) {
        Designation = designation;
    }

}

You can download source code here.

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