Square Otto:片段中未调用@Subscribe (Android)

Square Otto: @Subscribe is not called in fragment (Android)

我在 Android 中有一个抽象片段,我从 otto 总线执行注册和注销方法。我还有一个 @Subscribe 用于此 class.

中的方法
public abstract class MyBaseFrag extends Fragment {

    @Override
    public void onResume() {
        super.onResume();
        bus.register(this);
    }
    // unregister onPause...

    @Subscribe
    public void loadMapOnAnimationEnd(TransitionAnimationEndEvent event) {
        loadMap();
    }
}

我有另一个片段从这里延伸出来并且是它正在执行的片段。我发送了事件,但从未调用该方法。

public class MyFragment extends MyBaseFrag {
    // my code here..
}

问题:

我发送了事件,但从未调用该方法。

我意识到 @Subscribe 在抽象 class 中不起作用,但它在具体 class 中起作用。

Just moving the @Subscribe method into MyFragment (not abstract class) it just works:

public abstract class MyBaseFrag extends Fragment {

    @Override
    public void onResume() {
        super.onResume();
        bus.register(this);
    }
    // unregister onPause...
}


public class MyFragment extends MyBaseFrag {
    @Subscribe
    public void loadMapOnAnimationEnd(TransitionAnimationEndEvent event) {
        loadMap();
    }
}

有效!

这是一项功能 - 请参阅文档中的 Subscribing

Registering will only find methods on the immediate class type. Unlike the Guava event bus, Otto will not traverse the class hierarchy and add methods from base classes or interfaces that are annotated. This is an explicit design decision to improve performance of the library as well as keep your code simple and unambiguous.