Answer the question
In order to leave comments, you need to log in
How to pass an interface to an inherited class with a shared lifecycle?
Good afternoon. I have a scanner class which needs a fragment lifecycle to enable and disable like onPause and onResume. The scanner is planned to be used in different fragments. I inherited the scanner class itself from Fragment. And I inherit the class in which I want to use the scanner from the scanner class and also expand it with interfaces with callbacks from the scanner. interfaces are not initialized for obvious reasons. How can I do this so as not to clutter up the classes where the scanner should be used with unnecessary logic.
сlass Scanner extends Fragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof ScannerStatus){
this.scannerStatus = (ScannerStatus) context;
}
}
———————————————-
class A extends Scanner implements ScannerStatus
вот тут я хочу получать колбеки от Scanner
————————————————————
Answer the question
In order to leave comments, you need to log in
composition over inheritance.
You don't have to inherit all the time. It is necessary to add this Scanner as a childfragment (getChildFragmentManager()) to those places where it is needed. Also agree that parentFragment will implement the callback interface.
In addition, you need to understand whether the scanner itself should really be a fragment.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question