android - IllegalArgumentException when using Otto with a retained Fragment -
i using otto 1.3.3 , when resume application illegalargumentexception
following stacktrace:
caused by: java.lang.illegalargumentexception: producer method type class com.couchsurfing.mobile.ui.setup .sessionproviderfragment$sessionconnectionstatechangeevent found on type class com.couchsurfing.mobile.ui.setup.sessionproviderfragment, registered type class com.couchsurfing.mobile.ui.setup.sessionproviderfragment. @ com.squareup.otto.bus.register(bus.java:194) @ com.couchsurfing.mobile.ui.baseretainedfragment .oncreate(baseretainedfragment.java:20)
the sessionproviderfragment
has instance retained, please find below extended class:
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; } }
i tried both using bus.register(this)
in onattach()
or oncreate()
, didn't change issue.
the proper place register on bus in onresume()
, proper place unregister in onpause()
so:
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); } }
note ondestroy()
not guaranteed called.
you might comment on , say, hey chris, if register in onresume()
, and events fired before hit method won't receive events! right, means aren't using producers should be.
also note, if use roboguice-sherlock don't have inject yourself. don't need null
bus
when fragment goes out of scope garbage collector clean you.
Comments
Post a Comment