Monday, December 27, 2010

How to use Intent to pass object ?

During my eZeditor app development I figured out how to pass an object using intent from one activity to another.

Example:
  1. If you want to pass an Object (Note.java) from one activity to another.
  2. The Note.java should implement android.os.Parcelable interface.
  3. Before you say startActivity(intent) on the source activity to navigate.
  4. Say intent.putExtra("com.ez.editor.note", note); where note is an object.
  5. On the target activity this.getIntent().getParcelableExtra("com.ez.editor.note"); should return you the object that you intent to pass.
public class Note implements Parcelable {

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

public Note createFromParcel(Parcel in) {
return new Note(in);
}

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(name);
out.writeInt(index);
}

public Note[] newArray(int size) {
return new Note[size];
}

};
}

1 comment:

javin paul said...

Good stuff dude keep it up.

Thanks
Javin
deadlock in java