How to define parcelable of interface type in .aidl file?

Landschaft picture Landschaft · Oct 17, 2011 · Viewed 11.6k times · Source

I have an .aidl file that defines a single parcelable of an interface type, let's say

parcelable MyInterface;

Whereby MyInterface is a java interface declared in MyInterface.java that extends the Parcelable interface. The android parcelable mechanism requires me to define a static CREATOR in the parcelable class. But how can I do this for an interface since the interface class does not know the concrete implementation and therefor cannot implement the createFromParcel() method?

How will the android runtime decide which CREATOR (from which subclass) to call? Is it even impossible to use an interface type in an .aidl file?

Answer

Pankaj Arora picture Pankaj Arora · Jun 18, 2014

Implement the Parcelable over AIDL

First Step:- Create a different .aidl file that will be used for define the Student class (Parcelable class).

       (Student.aidl)
         package com.aidl; 
         parcelable Student;

we write this because aidl can detect Student class.

Second Step:- now you have to define a java class with name student and implement parcable interface in this class. parcable interface has two abstract method that you have to implement in your student class.

    import android.os.Parcel;
    import android.os.Parcelable;
    public class Student implements Parcelable {
            public String name;
            public String father_name;
            public Student(Parcel source)
            {
                            name = source.readString();
                            father_name = source.readString();
            }
            public Student()
            {}
            public void setName(String name)
            {
                            this.name = name;
            }
            public void setFatherName(String father_name)
            {
                            this.father_name = father_name;
            }

// methods of parcable interface

            @Override
            public int describeContents() {
                            // TODO Auto-generated method stub
                            return 0;
            }
            @Override
            public void writeToParcel(Parcel dest, int flags) {
                            // TODO Auto-generated method stub
                            dest.writeString(name);
                            dest.writeString(father_name);
                           
            }

In any class which is implementing Parcelable have to provide CREATOR field. The type of CREATOR must be Parcelable.Creator. Here in place of T we write the name of our class eg. Student. CREATOR is used during UnMarshalling of the object.

It has two methods –

1-T createFromParcel(Parcel parcel) :This method is called when UnMarshalling happen 
  during receiving the data. Keep care that  we receive the data member in same sequence
  as we write in writeToPacel(). Here we create a  constructor in which we demarshalling
  the data.
2-NewArray(int size) : Here we just create an array of given size and return.

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

for more info: Check Here