Thursday 23 August 2012

Passing Data Object from one Activity to Another

We often need to pass data model from one activity to another. For this Android provides a way to do this and it is called Parcelable.

Using parcelable we can pass different types of data:

1. int and int[]
2. float and float[]
3. double and double[]
4. byte and byte[]
5. boolean[]
6. long and long[]
7. String and String[]
8. Object and Object[]
9. List and List<T>
10. Map
11. Nested data model and many more.

Below I am covering some basic types and rest you can try on your own.

Create a data Model: TestModel


public class TestModel implements Parcelable{

public int testInt;
public Double testDouble;
public String testString;
public boolean testBoolean;
private byte testByte;

public TestModel() {
testInt = 0;
testDouble = 0.0;
testString = "";
testBoolean = false;
testByte = 0;
}

//Get your data here in the same order in which you have set it. public TestModel(Parcel in) {
testInt = in.readInt();
testDouble = in.readDouble();
testString = in.readString();
boolean[] flag = new boolean[1];
in.readBooleanArray(flag);
testBoolean = (flag[0] == true)? true : false;
testByte = in.readByte();
}

public int getTestInt() {
return testInt;
}

public void setTestInt(int testInt) {
this.testInt = testInt;
}

public Double getTestDouble() {
return testDouble;
}

public void setTestDouble(Double testDouble) {
this.testDouble = testDouble;
}

public String getTestString() {
return testString;
}

public void setTestString(String testString) {
this.testString = testString;
}

public boolean isTestBoolean() {
return testBoolean;
}

public void setTestBoolean(boolean testBoolean) {
this.testBoolean = testBoolean;
}

public byte getTestByte() {
return testByte;
}

public void setTestByte(byte testByte) {
this.testByte = testByte;
}

//Write your data here

public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(testInt);
dest.writeDouble(testDouble);
dest.writeString(testString);
dest.writeBooleanArray(new boolean[]{testBoolean});
dest.writeByte(testByte);

}

public static final Parcelable.Creator<TestModel> CREATOR = new Parcelable.Creator<TestModel>()
{
public TestModel createFromParcel(Parcel in)
{
return new TestModel(in);
}
public TestModel[] newArray(int size)
{
return new TestModel[size];
}
};

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

}

Passing data model to another Activity:
//Setting data in the model
TestModel  model = new TestModel ();
//Set values in the model...

//Create intent

Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();                //create bundle..
bundle.putParcelable("data",  model  );    
intent.putExtras(bundle);                    //set the bundle..
startActivity(intent);

Get the data in the Activity:




Bundle bundle = getIntent().getExtras();
TestModel  model = bundle.getParcelable("data");


profile for Vineet Shukla at Stack Overflow, Q&A for professional and enthusiast programmers