Search code examples
androidparcelableaidl

Can a Parcelable contain an AIDL Interface?


I have a Service that communicates via AIDL. I have successfully created a callback interface and use a class that implements Parcelable.

I want to have the Parcelable contain a reference to the Interface defined in the AIDL file but I get the error that my callback class can't be converted to Parcelable. "incompatible types: ICallback cannot be converted to Parcelable"

My Parcelable looks like this

class Foo implements Parcelable {
   String someString;
   ICallback callback;
....
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(someString);
        dest.writeParcelable(callback, flags); //doesn't work
    }

}

The AIDL for the service has a method like:

void register(Foo[] someFoos);

Solution

  • My understanding is that your ICallback is a Binder interface, so you have a declaration of the form interface ICallback { ... } in some AIDL file.

    In this case, you can write it to a Parcel using parcel.writeStrongBinder(callback.asBinder()) or with the convenience method parcel.writeStrongInterface(callback);. On the receiving end, it can be retrieved with:

    callback = ICallback.Stub.asInterface(parcel.readStrongBinder());

    API Reference:

    Examples from Android Framework: