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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 182
    Comment on it

    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];
                }
            };
        }
    
    

    PASS the complete class in INTENT

          intent.putExtra("student",student);
    
    

    // Get data from a parcelable object

      Student student = getIntent().getParcelableExtra("student");
    

    NOTE :Reading and writing the value into parcel should be in same order ,otherwise you will get incorrect values.

 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: