拥有一个单例dataManager对象,该对象在其构造函数中注册OTTO侦听器以侦听数据就绪事件。 在应用程序中,dataManager在任何地方都被引用为
DataManager.instance.functionOfIt()
如果没有为此dataManger调用GlobalOttoBus.instance.unregister(this),那么它的侦听器函数(fun getMessage(event:DataEvent))可能会在重新打开应用程序后多次调用。
class DataManager {
private var mApplication: Application? = null
companion object {
val TAG = DataManager::class.java.name
private var dataMgr: DataManager? = null
val instance: DataManager?
@Synchronized get() {
return dataMgr
}
}
constructor(application: Application) {
mApplication = application
dataMgr = this
Log.e(TAG, "+++ DataManager:ctor(), GlobalOttoBus.instance.register()");
// have no way to to unregister in this DataManager ??? ???
GlobalOttoBus.instance.register(this)
}
@Subscribe
fun getMessage(event: DataEvent) {
Log.e(Tag, "+++ dataMgr::getMessage(), ${this}, ${event}")
onDataReady(event)
}
… …
}
数据准备就绪后,它会将数据发布到otto bus:
override fun onResponse(call: Call?, response: Response?) {
if (response!!.isSuccessful()) {
// parse the data ……
// then post data ready event
var dataEvt: DataEvent = DataEvent()
dataEvt.setPostData(posts)
Log.d(“GsonParse”, "+++ onResponse(), call GlobalOttoBus.instance.post(dataEvt): ${dataEvt} ")
GlobalOttoBus.instance.post(dataEvt)
}
当最小化app(os破坏应用程序)然后重新打开app时,观察到这个DataManager的构造函数被调用(这个新会话中的一个新实例),所以它在它的构造函数中再次执行OTTO注册器监听器。
问题是从OTTO取消注册的位置,还是更多如何管理这个单例DataManager的生命周期?在最小化应用程序然后重新打开应用程序后,似乎前一个应用程序仍处于活动/监听状态,但不在新应用程序的范围内。
这是追踪
第一次启动app,构造函数:
08-19 11:32:33.558 5296-5296/xxxapplication E/DataManager: +++ DataManager:ctor(), GlobalOttoBus.instance.register(), this: DataManager@94fd499
在最小化应用程序并重新打开应用程序后,再次调用构造函数新实例
08-19 11:34:14.141 5296-5296/xxxapplication E/DataManager: +++ DataManager:ctor(), GlobalOttoBus.instance.register(), DataManager@661f37d
一个帖子:
08-19 11:34:15.242 5296-5380/xxxapplication W/GsonParse: +++ onResponse(), call GlobalOttoBus.instance.post(dataEvt): DataEvent@f5a0df6
两个侦听器调用不同的dataManager实例:
08-19 11:34:15.242 5296-5380/xxxapplication E/DataManager: +++ dataMgr::getMessage(), DataManager@94fd499, DataEvent@f5a0df6
08-19 11:34:15.395 5296-5380/xxxapplication E/DataManager: +++ dataMgr::getMessage(), DataManager@661f37d, DataEvent@f5a0df6
答案 0 :(得分:0)
找到一种解决方法,在mainActivity中实现LifecycleRegistryOwner,所以在lifeCycle Lifecycle.Event.ON_DESTROY上,这意味着活动被终止,因此取消注册DataManger的otto监听器。
class MainActivity : AppCompatActivity(), LifecycleRegistryOwner {
private val mRegistry: LifecycleRegistry = LifecycleRegistry(this);
private var theLifeCycleObserve: TheLifeCycleObserve? = null
override fun getLifecycle() : LifecycleRegistry {
return mRegistry
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<Toolbar>(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
……
theLifeCycleObserve = TheLifeCycleObserve(lifecycle, object : TheLifeCycleObserve.OnLifeCycleChange {
override fun onStar() {
}
override fun onDestroy() {
DataManager.instance!!.unregisterOttoBus()
lifecycle.removeObserver(theLifeCycleObserve)
}
})
lifecycle.addObserver(theLifeCycleObserve)
}
………
}
class TheLifeCycleObserve(private var lifecycle: LifecycleRegistry?, private var lifeCycleChangeListener: OnLifeCycleChange?) : LifecycleObserver {
interface OnLifeCycleChange {
fun onStar()
fun onDestroy()
}
init {}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
lifeCycleChangeListener!!.onStar()
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
lifeCycleChangeListener!!.onDestroy()
}
}