Simple BusProvider.getInstance().post()
bring exception not main thread
.
How to send event from Service to Activity with Otto event bus?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Best implementation custom bus class for me
To post from any thread (main or background) and receive on the main thread, use the following
MainThreadBus
instead of a vanillaBus
This is based on Andy Dennie's answer.
There is no need to both extend and wrap a
Bus
object, do one or the other. In Dennie's answer, which is effectively a wrapper, theBus
base class is just being used like an interface, all the functionality is overwritten.It would work even if you removed the
Bus
base class unless you happened to reference theMainThreadBus
via aBus
reference.bus = new Bus(ThreadEnforcer.ANY); is the clear solution to this problem. Its all you have to do.
To post from any thread (main or background) and receive on the main thread, try something like
Note: credit goes to Jake Wharton and "pommedeterresaute" at https://github.com/square/otto/issues/38 for the general approach. I just implemented it with a wrapper class rather than a subclass.
I did it simply:
Happy coding.. :)
Just create the
BasicBus
withThreadEnforcer.NONE
to post event from non-main threads. The mentionedThreadEnforcer.MAIN
is exactly the opposite (and the default), which accepts only posts from the main-thread.