哪种Activity生命周期方法最适合在事件总线上注册/注销? [英] Which Activity lifecycle methods are best to register/unregister to event bus?

查看:122
本文介绍了哪种Activity生命周期方法最适合在事件总线上注册/注销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Activity中注册取消注册的最佳位置是什么?为什么?

What is the best place to register and unregister to an event bus (like otto, EventBus, or tinybus) in an Activity and why?

  1. onCreate()-onDestroy()
  2. onStart()-onStop()
  3. onResume()-onPause()

奥托(Otto)的示例使用onResume()-onPause(),EventBus提及onStart()-onStop(),我们需要在应用中使用onCreate()-onDestroy()来更新活动的UI,即使该活动位于用户界面中也是如此.背景. 因此,我想这可能是事件的性质及其处理方式中的任何三个,但是我想知道是否还有其他需要考虑的地方.

Otto's example uses onResume()-onPause(), EventBus's mentions onStart()-onStop(), and we needed to use onCreate()-onDestroy() in our app to update the activity's UI even when it was in the background. So I guess it can be any of the three depending on the nature of the events and their handling, but I was wondering if there is anything more to it that should be considered.

推荐答案

@levavare,我认为注册/注销的正确时间取决于您的事件以及您打算如何处理.并且对于同一应用程序中的不同事件可以是不同的.

@levavare, I think the correct time to register/unregister depends on your events and what you intend to do with them. And can be different for different events within the same application.

例如,我在一个Android应用中使用EventBus,该应用通过蓝牙监视实时数据记录设备(在本例中为Arduino).我有两种截然不同的事件类型.

For example, I'm using EventBus in an Android app that monitors a realtime data logging device (Arduino, in this case) via Bluetooth. I have two quite different types of events.

第一个事件由我的蓝牙代码发布,以通知我的一个片段通知已从设备接收到一组新的仪器读数.然后,该片段将它们写入数据库表.重要的是始终听到并采取行动.片段在其OnCreate/OnDestroy方法中进行注册/注销.我也以较高的优先级订阅该活动.

The first event is posted by my Bluetooth code to notify one of my fragments that a new set of instrument readings has been received from the device. That fragment then writes them to a database table. It's important that event always be heard and acted on. The fragment registers/unregisters in its OnCreate/OnDestroy methods. I also subscribe for that event with elevated priority.

将新记录添加到数据库时,另一个事件由数据库层发布.我有一系列片段,显示了读数的不同子集(温度,压力,警报条件).当查看这些片段之一时,应在数据库中读取新读数后立即对其进行更新.但是,当片段不可见时,就没有理由对其进行读取了.我在OnStart/OnStop中注册/注销了那些片段.我打算在OnResume/OnPause中完成该工作,坦率地说,我认为它对于我的应用程序同样适用.但是@Jordy的答案和链接说服我改用OnStart/OnStop.

The other event is posted by the database layer when the new record is added to the database. I have a series of fragments that show different subsets of the readings (temperatures, pressures, alarm conditions). When one of those fragments is being viewed it should update as soon as the new reading is in the database. But when the fragment is out of sight, there's no reason for it to act on a reading. I have those fragments register/unregister in OnStart/OnStop. I was going to do that work in OnResume/OnPause and, frankly, I think it would work as well there for my app. But @Jordy's answer and link convinced me to go with OnStart/OnStop instead.

这篇关于哪种Activity生命周期方法最适合在事件总线上注册/注销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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