Casting Interface in Activity

Viewed 4853

The matter is that, before this time, I implemented interfaces in Activity, values ​​of methods set in Fragment. That is, in onAttach() initialized, and then where to call the interface method called. Example:

interface OnNumberChangeCallback{
    void onSuccess();
}

In Fragment:

OnNumberChangeCallback onNumberChangeCallback;

onAttach(Context context){
    onNumberChangeCallback = (OnNumberChangeCallback) context;
}

etc.

I want to initialize the interface in BaseActivity. And in Fragment to implement this interface. When I try to initialize in onCreate method this way:

onNumberChangeCallback = (OnNumberChangeCallback) this;

I get the error:

BaseActivity cannot be cast to OnNumberChangeCallback

Question: How to properly initialize the interface in Activity?

2 Answers
Related