Component design idea:
By observing theLifeOwner(Activity)
theLifeCycle Event
In the ActivityonDestroy
And release all of itDisposable
Learn more about LifeOwner in this article: Introduction to Android Architecture Components
Usage:
In activities and Fragments
RxBus
.toObservable(SynEvent::class.java)
.subscribe {
...
}
.disposeOnDestroy(this)
Copy the code
All activities in the project inherit from AppCompatActivity. AppCompatActivity is itself a LifeOwner.
The Fragment itself is also the LifeOwner object, used the same way as above.
In the View of
RxBus
.toObservable(SynEvent::class.java)
.subscribe {
...
}
.disposeOnDestroy(context as AppCompatActivity)
Copy the code
For a View that relies on an Activity, the Context is an AppCompatActivity (compatactivity), so there is a strong compatactivity.
Does the view context have to be an Activity? Check out this article:
Does view.getContext () necessarily return an Activity object?
That is, on systems 5.0 and aboveAvctivity
, i.e.,LifeOwner
, so we still need to pay attention to this strong rotation.
PS: Currently our project minSdkVersion is 21. If it’s not, you can’t use it that way.
In the Presenter
RxBus
.toObservable(SynEvent::class.java)
.subscribe {
...
}
.disposeOnDestroy(view.lifeContext())
Copy the code
Since all MVPS in our project inherit views from the following interface:
interface BaseLifeCycleView {
fun lifeContext(): AppCompatActivity
}
Copy the code
So view.lifecontext () above is the LifeOwner.
In the Application
RxBus
.toObservable(SynEvent::class.java)
.subscribe {
...
}
.disposeOnDestroy(ProcessLifecycleOwner.get())
Copy the code
ProcessLifecycleOwner is also a component in Android Architecture Components that can be used to observe the entire app lifecycle.
DisposeOnStop extension function
This is used in the same way as disposeOnDestroy, except that all disposables are released on OnStop.
Does not support
Service, BroadcastReceiver, and ContentProvider are not supported because they are not lifeOwners. But you can simply inherit it and make it LifeOwner yourself.
Realize the principle of
The implementation principle is simple:
aLifeOwner
Object to create aLifeObserver
It holdsLifeOwner
All of theDisposable
. inLifeOwner's Lifecycle. Event. ON_DESTROY
, releaseLifeOwner
All of theDisposable
There are two main components:
DestroyLifeCycleObserver
It is a LifecycleObserver that holds the LifecycleOwner and is responsible for all of its Disposable releases.
internal class DestroyLifeCycleObserver(val lifeOwner: LifecycleOwner) : LifecycleObserver {
private val disposableList = ArrayList<Disposable>()
var requestRemoveLifecycleObserver: RequestRemoveLifecycleObserver? = null
init {
lifeOwner.lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
LogUtils.d(TAG, "${getKey()} OnLifecycleEvent ON_DESTROY , disposableList.size : ${disposableList.size}")
disposableList.forEach {
if(! it.isDisposed) { it.dispose() } } requestRemoveLifecycleObserver? .requestRemoveDestroyObserver(this) } fun addDisposable(disposable: Disposable) {if (disposable.isDisposed) return
disposableList.add(disposable)
}
fun getKey() = lifeOwner.toString()
}
Copy the code
GlobalRxDisposeManager
DestroyLifeCycleObserver Is responsible for maintaining all lifecycleObservers, using the DestroyLifeCycleObserver example:
object GlobalRxDisposeManager { private val rxLifecycleObservers = HashMap<String, DestroyLifeCycleObserver? >() fun getLifecycleObserver(key: String): DestroyLifeCycleObserver? {return rxLifecycleObservers[key]
}
fun addLifecycleObserver(lifeCycleObserver: DestroyLifeCycleObserver) {
rxLifecycleObservers[lifeCycleObserver.getKey()] = lifeCycleObserver
lifeCycleObserver.requestRemoveLifecycleObserver = object : RequestRemoveLifecycleObserver {
override fun requestRemoveDestroyObserver(observer: DestroyLifeCycleObserver) {
destroyLifeCycleObserver.remove(observer.getKey())
LogUtils.d(TAG, "destroyLifeCycleObserver size : ${destroyLifeCycleObserver.size}")}... }}... }Copy the code
DisposeOnDestroy extension function
Combine GlobalRxDisposeManager and DestroyLifeCycleObserver and simplify use:
fun Disposable.disposeOnDestroy(lifeOwner: LifecycleOwner): Disposable {
LogUtils.d(TAG, "life owner key : ${lifeOwner}")
var lifecycleObserver = GlobalRxDisposeManager.getDestroyObserver(lifeOwner.toString())
if (lifecycleObserver == null) {
lifecycleObserver = DestroyLifeCycleObserver(lifeOwner)
GlobalRxDisposeManager.addDestroyObserver(lifecycleObserver)
}
lifecycleObserver.addDisposable(this)
return this
}
Copy the code
Source: RxLifeCycleExtensions
Android Advanced – LifeOwner based RxJava Memory Leak solution