2

I am trying to implement some common logic and reaction to some events in base class of all my dialogues.

And registering and unregistering in EventBus, and catching some events in base class.

So when I tried to instantiate an instance of derived class - EventBus throws an exception that DerivedClass has no methods like onEvent(*). I don't want to add some stub onEvent methods in every derived class, it is not the way software development should be.

It is so sad, if there is no way to use such approach about inheritance.

Did someone faced that?

TsimoX
  • 447
  • 5
  • 14

2 Answers2

1

You could make a protected method(or abstract class with abstract method) in the base class that you could override in child class(if needed), before registering EvenBus.

public class Test extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(doIneedEventBus()){
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(doIneedEventBus()){
            EventBus.getDefault().unregister(this);
        }
    }

    protected boolean doIneedEventBus() {
        return true;
    }
}

Child class:

public class TestChild extends Test {

    @Override
    protected boolean doIneedEventBus() {
        return false;
    }
}

Second option:

 try {
            EventBus.getDefault().register(this);
        } catch (Throwable t){
            t.printStackTrace();
        }

Or you could wait until this issue is fixed in the library - https://github.com/greenrobot/EventBus/issues/58

Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
  • Sure I can. Anyway it enforce me to change _every_ child class, and I have many of them. – TsimoX Aug 28 '15 at 08:53
  • if you make the superclass abstract and the method abstract, the IDE will at least show all the places you need to implement the method. – Mikelis Kaneps Aug 28 '15 at 08:57
  • it is workaround. and it is not solving the main problem - you have no onEvent method in child class (all common events handled in base class), so suggested code will receive exception too. – TsimoX Aug 28 '15 at 09:09
  • It is solving the problem, if your child class doesn't need any onEvent, override doIneedEventBus and return false, if does need it don't override it and just add the OnEvent with the object you want to recieve. It solves the problem, other only possibility is to make a try{} catch on EventBus.register – Mikelis Kaneps Aug 28 '15 at 09:14
  • child needs it. imagine common event (like close everything), and it is handled in base class. and base class in costructor (on OnCreate) does EventBus.register(this). But "this" will be of child class type. And child class has no onEvent. – TsimoX Aug 28 '15 at 09:26
  • 1
    yes i now fully understand your problem. You could put this logic in the activity and remove it from dialog or wait for the issue to be completed. – Mikelis Kaneps Aug 28 '15 at 09:44
0

Use the rxbus2 library, which is compatible with base classes.

https://github.com/warrenth/RxBus2