Monday, August 5, 2013

Send one object from one activity to another activity

Ceate a class name ObjectA which have implments Parcelable interface

1. ObjectA

you package name:

import android.os.Parcel;
import android.os.Parcelable;

/**
 * *  A basic object that can be parcelled to transfer between objects *
 */
public class ObjectA implements Parcelable {
private String strValue;
private Integer intValue;

/** * Standard basic constructor for non-parcel * object creation */
public ObjectA() {
System.out.println("------I m in constr--");
}

/**
* * * Constructor to use when re-constructing object * from a parcel * * @param
* in a parcel from which to read this object
*/
public ObjectA(Parcel in) {
System.out.println("------I m in override constr--");
readFromParcel(in);
}

/** * standard getter * * @return strValue */
public String getStrValue() {
return strValue;
}

/** * Standard setter * * @param strValue */
public void setStrValue(String strValue) {
System.out.println("------here set Str--"+strValue);
this.strValue = strValue;
}

/** * standard getter * * @return */
public Integer getIntValue() {
return intValue;
}

/** * Standard setter * * @param intValue */
public void setIntValue(Integer intValue) {
System.out.println("------here set Intr--"+intValue);
this.intValue = intValue;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) { // We just need to write
// each field into the
// // parcel. When we
// read from parcel,
System.out.println("------Here write data....--"); // they
// will come back in the
// same order
dest.writeString(strValue);
dest.writeInt(intValue);
}

/**
* * * Called from the constructor to create this * object from a parcel. *
* * @param in parcel from which to re-create object
*/
private void readFromParcel(Parcel in) { // We just need to read back each
// // field in the order that it
// was
System.out.println("------read from parcel--"); // written to the parcel
strValue = in.readString();
intValue = in.readInt();
}


@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{

public ObjectA createFromParcel(Parcel in)
{
System.out.println("------In createParcel--");
return new ObjectA(in);
}

public ObjectA[] newArray(int size) {
System.out.println("------in cearet parcel array--");
return new ObjectA[size];
}

};

}

2. Write below lines from which object have to be sent.

                        ObjectA ob=new ObjectA();
              System.out.println("------before put value--");
              ob.setIntValue(30);
              ob.setStrValue("Praveen");
              System.out.println("------after put value--");
                Intent i = new Intent();
                i.putExtra("object",ob);
                i.setClass(getBaseContext(),RegisterActivity.class);
                startActivity(i);


3. Get the particular parcelled object in another activity.


        Bundle b=getIntent().getExtras();
        ObjectA ob=b.getParcelable("object");
        System.out.println("--------data from string object-----"+ob.getStrValue());
        System.out.println("--------data from int object-----"+ob.getIntValue());

0 comments:

Post a Comment