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?
The Android Interface Definition Language (AIDL) is a tool that lets users abstract away IPC. Given an interface (specified in a . aidl file), various build systems use the aidl binary to construct C++ or Java bindings so that this interface can be used across processes, regardless of the runtime or bitness there.
AIDL has been around longer than HIDL, and is used in many other places, such as between Android framework components or in apps. Now that AIDL has stability support, it's possible to implement an entire stack with a single IPC runtime. AIDL also has a better versioning system than HIDL.
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
about use interface in AIDL file: I don't think there is anything there stopping you to do so. Because "parcelable MyInterface;" does not actually generate anything in gen folder, it is just needed for function signature of any AIDL interface using this MyInterface type.
CREATOR You have to add creator definition for all your classes implements android.os.Parcelable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With