over 9 years ago
In simple words , Send the complete object to other views like (switching b/w different Activity).
What if you need to send 50 value to other Activity, Don't write 50 putExtra to your code as it will increase the size of code ,just use Parcelable
Ignore sending every fields in putExtra to other Activity , use Parcelable to send Complete object to other Class.
IMPLEMENTING YOUR CLASS WITH PARCELABLE
- public class Student implements Parcelable{
- String Name;
- int Age;
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- /**
- * Storing the Student data to Parcel object
- **/
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(Name);
- dest.writeInt(Age);
- // write as much as variable you need
- }
- /**
- * A constructor that initializes the Student object
- **/
- public Student(String sName, int sAge){
- this.Name = sName;
- this.Age = sAge;
- }
- /**
- * Retrieving Student data from Parcel object
- * This constructor is invoked by the method createFromParcel(Parcel source) of
- * the object CREATOR
- **/
- private Student(Parcel in){
- this.Name = in.readString();
- this.Age = in.readInt();
- }
- public static final Parcelable.Creator
CREATOR = new Parcelable.Creator () { - @Override
- public Student createFromParcel(Parcel source) {
- return new Student(source);
- }
- @Override
- public Student[] newArray(int size) {
- return new Student[size];
- }
- };
- }
public class Student implements Parcelable{ String Name; int Age; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } /** * Storing the Student data to Parcel object **/ @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(Name); dest.writeInt(Age); // write as much as variable you need } /** * A constructor that initializes the Student object **/ public Student(String sName, int sAge){ this.Name = sName; this.Age = sAge; } /** * Retrieving Student data from Parcel object * This constructor is invoked by the method createFromParcel(Parcel source) of * the object CREATOR **/ private Student(Parcel in){ this.Name = in.readString(); this.Age = in.readInt(); } public static final Parcelable.CreatorCREATOR = new Parcelable.Creator () { @Override public Student createFromParcel(Parcel source) { return new Student(source); } @Override public Student[] newArray(int size) { return new Student[size]; } }; }
PASS the complete class in INTENT
// Get data from a parcelable object
NOTE :Reading and writing the value into parcel should be in same order ,otherwise you will get incorrect values.
0 Comment(s)