Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Parcelable Android Example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 325
    Comment on it

    Parcelable is mechanism for passing data between Activities. Parcelable works same like serialization but It is faster then serialization.

    Here are the points to make class parcelable.

    1. Create simple model class like below I created with name Student.

    public class Student {
        private String name;
        private String adrress;
        private int id;
    }

    2. Implement class with Parcelable interface and add methods.

    public class Student implements Parcelable{
        private String name;
        private String adrress;
        private int id;
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel parcel, int i) {
    
        }

    3. Make a Creator and override methods.

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

    4. Create new Constructor of Student class which has Pacel type argument.

    protected Student(Parcel in) {
            this.name = in.readString();
            this.adrress = in.readString();
            this.id = in.readInt();
        }

    5. From writeToParcel() write your variables to Parcel parameter.

     @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.name);
            dest.writeString(this.adrress);
            dest.writeInt(this.id);
        }

     

    Full working class of Student is :-
     

    public class Student implements Parcelable {
        private String name;
        private String adrress;
        private int id;
    
        public Student() {
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.name);
            dest.writeString(this.adrress);
            dest.writeInt(this.id);
        }
    
        protected Student(Parcel in) {
            this.name = in.readString();
            this.adrress = in.readString();
            this.id = in.readInt();
        }
    
        public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
            @Override
            public Student createFromParcel(Parcel source) {
                return new Student(source);
            }
    
            @Override
            public Student[] newArray(int size) {
                return new Student[size];
            }
        };
    }
    

     

    Now you can pass object from one activity to another and you can also pass ArrayList of Student to another activity.

     

    Happy Coding :D
     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: