如何在Android中使用事件总线库 [英] How to use Event bus library in android

查看:60
本文介绍了如何在Android中使用事件总线库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用事件总线库,我无法理解它的功能,并看到多个示例.是仅使用服务类还是使用活动和片段,然后使用活动,那么我们在活动或片段中使用事件总线的情况是什么?

I'm try to use event bus lib, I could not understand it's functionality and see multiple examples. Is it use only services class or use activity and fragment then is use activity what situation we use event bus in activity or fragment.

推荐答案

事件的优点之一是,您可以将对象传递到某处",而无需知道在何处拾取对象.这使将活动或片段状态推送到控制器类,然后使用从该控制器发送的事件回到活动或片段很容易.

One of the advantages of events is that you can pass objects around to 'somewhere', and you don't need to know where it is picked up. This makes it easy to push your activity or fragment state to a controller class, and then use events sent from this controller back to the activity or fragment.

由于事件的缘故,控制器不需要知道接收端是活动还是片段.这使得切换一个到另一个非常容易.另一方面,这也使得将控制器轻松放置在其他位置变得容易.像首先在应用程序类中将其作为实例,然后将其移至服务.

Because of events, the controller doesn't need to know if the receiving end is an activity or a fragment. This makes it very easy to switch one for the other. On the other hand this also makes it easy to put the controller somewhere else. Like first you have it as an instance in the application class, and then you move it to a service.

我写了一篇文章,上面有一个非常具体的示例,说明如何使用事件来处理如上所述的Android生命周期:

I wrote an article with a very concrete example on how you can use events to deal with the Android life cycle as described above: https://medium.com/@JuliusHuijnk/beating-the-android-life-cycle-d00a2f3ed88

这篇关于如何在Android中使用事件总线库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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