抛出:IllegalArgumentException奥托事件总线片段实例 [英] IllegalArgumentException with Otto Event bus in Fragment instance

查看:264
本文介绍了抛出:IllegalArgumentException奥托事件总线片段实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的奥托事件总线订阅的ListFragment某些事件。总线实例被存储并创建在应用的一个子类,换句话说,它应总线工作作为一个单。看起来这是不是个例......

I am using Otto Event bus to subscribe to certain events in a ListFragment. The bus instance is stored and created in an subclass of Application, in other words, it bus should work as a singleton. It seems like this is not a case...

该片段注册到总线 onActivityCreated(捆绑)和注销的的onDestroy()。这不,因为它应该工作。我从设备得到了一些崩溃报告,其中呼吁时,取消注册应用程序崩溃()(java.lang.IllegalArgumentException:如果缺少事件处理程序的注释方法...)。如果取消注册()调用之前调用任何注册()此异常仅抛出,或者如果取消注册()被称为两次。这可能只发生,如果...

The fragment is registering to the bus in onActivityCreated(Bundle) and unregistering in onDestroy(). This does not work as it should. I have gotten several crash reports from devices where the app crashes when calling unregister() (java.lang.IllegalArgumentException: Missing event handler for an annotated method...). This exception is only thrown if unregister() is called before any call to register(), or if unregister() is called twice. This may only happen if...

  • onActivityCreated(捆绑)的onDestroy不叫()
  • 的onDestroy()被调用了两次。
  • 应用程序实例调用之间重新创建 onActivityCreated(捆绑)的onDestroy()
  • onActivityCreated(Bundle) is not called before onDestroy().
  • onDestroy() is called twice.
  • The Application instance is recreated between between the call to onActivityCreated(Bundle) and onDestroy().

我的应用程序类:

public class App extends Application {

    private static App sInstance;

    private Bus bus;

    public static App getInstance() {
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        bus = new Bus(ThreadEnforcer.ANY);
    }

    public Bus getEventBus() {
        return bus;
    }

}

分片类:

public class MyFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        App.getInstance().getEventBus().register(this);
    }

    @Subscribe
    public void onEvent(MyEvent event) {
        ....
    }

    @Override
    public void onDestroy() {
        App.getInstance().getEventBus().unregister(this);
        super.onDestroy();
    }
}

更新:

我遗漏了一个重要的细节;该片段在 ViewPager 使用。他们被实例化的需求,在 ViewPager 页面之间的用户幻灯片。这个小细节,似乎改变在某些设备上的片段生命周期: onActivityCreated()从不要求后启动的片段 ViewPager 创建。

I left out an important detail; the fragments were used in a ViewPager. They are instantiated on demand as the users slides between the pages in the ViewPager. This minor detail seems alter the fragments life-cycle on some devices: onActivityCreated() is never called for fragments initiated after the ViewPager is created.

推荐答案

我有同样的问题。实例呆登记在总线的情况。一个可靠的解决方案是使用 ONSTART() / 的onStop()的方法来注册/注销接收器。这是广场人建议过。他们解释这样。如果活动是在背景,它不需要刷新横竖UI,因为用户界面是不可见的。一旦活动来到前台,将得到更新和刷新用户界面。

I had same issue. Instance stayed registered in the bus in some cases. A reliable solution is to use onStart()/onStop() methods to register/unregister receivers. This is what Square guys suggest too. They explain it like this. If activity is in background, it does not need to refresh UI anyway, because UI is not visible. Once activity comes foreground, it will receive update and refresh UI.

更新:中提到的意见,登记/ IN onResume注销() / 的onPause()可能会导致在某些情况下,一些意想不到的效果一样,如果没有显示在你的活动一个对话框,然后活动被暂停,不能再接收事件。

Update: as mentioned in the comments, registering / unregistering in onResume()/onPause() can cause some undesired effects in certain cases like if there is a dialog shown over your activity, then activity gets paused and is not able to receive events anymore.

这篇关于抛出:IllegalArgumentException奥托事件总线片段实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆