7

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?

Landschaft
  • 1,217
  • 1
  • 12
  • 12

2 Answers2

3
  1. 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.

  2. CREATOR You have to add creator definition for all your classes implements android.os.Parcelable.

zhao chang
  • 211
  • 3
  • 8
0

I ran into similar scenario, and wanted to share what I did. I had following aidl main interface which contains another interface inside it.

//ICounterAidlInterface.aidl

import path.to.aidldirectory.CounterListener

interface ICounterAidlInterface {
    int getCounter();
    void setCounterListener(CounterListener listener); 
}

Don't forget the import.

The question is how to represent this new type: CounterListener. Since CounterListener itself is an interface, you don't need to mark it parcelable.

You need to create another aidl file for the CounterListener too. So, I created another aidl file:

//CounterListener.aidl
interface CounterListener{
    void newData(int data);
}

Hope this helps :)