我在用奥托1.3.3 http://square.github.io/otto/当我恢复我的申请时,有时我会得到一个IllegalArgumentException
具有以下堆栈跟踪:
Caused by: java.lang.IllegalArgumentException: Producer method for type class
com.couchsurfing.mobile.ui.setup
.SessionProviderFragment$SessionConnectionStateChangeEvent found on
type class com.couchsurfing.mobile.ui.setup.SessionProviderFragment,
but already registered by type class
com.couchsurfing.mobile.ui.setup.SessionProviderFragment.
at com.squareup.otto.Bus.register(Bus.java:194)
at com.couchsurfing.mobile.ui.BaseRetainedFragment
.onCreate(BaseRetainedFragment.java:20)
The SessionProviderFragment
保留了它的实例,请在下面找到扩展类:
public abstract class BaseRetainedFragment extends SherlockFragment {
@Inject
Bus bus;
@Override
public void onCreate(final Bundle state) {
super.onCreate(state);
((CouchsurfingApplication) getActivity().getApplication()).inject(this);
setRetainInstance(true);
bus.register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
bus.unregister(this);
bus = null;
}
}
我尝试使用bus.register(this)
in onAttach()
or onCreate()
,这并没有改变问题。
在巴士上登记的正确地点是onResume()
注销的正确位置是onPause()
像这样:
public abstract class BaseRetainedFragment extends RoboSherlockFragment {
@Inject private Bus bus;
@Override
public void onCreate(final Bundle state) {
super.onCreate(state);
setRetainInstance(true);
}
@Override
public void onResume() {
super.onResume();
bus.register(this);
}
@Override
public void onPause() {
super.onDestroy();
bus.unregister(this);
}
}
注意onDestroy()
is 不保证被调用 http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29.
你可能会对此发表评论并说,嘿克里斯,如果我注册onResume()
并且在我点击此方法之前事件被触发,我不会收到事件!你是对的,但这意味着你没有使用制片人 http://square.github.io/otto/就像你应该的那样。
另请注意,如果您使用机器人夏洛克 https://github.com/rtyley/roboguice-sherlock/blob/master/src/main/java/com/github/rtyley/android/sherlock/roboguice/fragment/RoboSherlockFragment.java您不必自己注射。你也不需要null
the Bus
当片段超出范围时,垃圾收集器将为您清理它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)