LifecycleDisposable

LifecycleDisposable dispose RxJava streams on lifecycle down event that corresponding to Activity/Fragment's lifecycle state when subscribe using Android Architecture Components Lifecycle.

Usage

Activity

class MainActivity : FragmentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Observable.just("LifecycleDisposable")
            .subscribe()
            .disposeOnLifecycle(this) // dispose when onDestroy is called
    }
}

Table 1 Corresponding between Activity's lifecycle state and Lifecycle down event.

Subscribe Lifecycle.State Lifecycle.Event Dispose
onCreate INITIALIZED ON_DESTROY onDestroy
onStart CREATED ON_STOP onStop
onResume STARTED ON_PAUSE onPause
onPause STARTED ON_DESTROY onDestroy
onStop CREATED ON_DESTROY onDestroy
onDestroy DESTROYED ON_DESTROY onDestroy

Fragment

class MainFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Observable.just("LifecycleDisposable")
            .subscribe()
            .disposeOnLifecycle(this) // dispose when onDestroy is called
    }
}

Table 2 Corresponding between Fragment's lifecycle state and Lifecycle down event.

Subscribe Lifecycle.State Lifecycle.Event Dispose
onCreate INITIALIZED ON_DESTROY onDestroy
onCreateView INITIALIZED ON_DESTROY onDestroyView
onStart CREATED ON_STOP onStop
onResume STARTED ON_PAUSE onPause
onPause STARTED ON_DESTROY onDestroyView
onStop CREATED ON_DESTROY onDestroyView
onDestroyView DESTROYED ON_DESTROY onDestroyView
onDestroy DESTROYED ON_DESTROY onDestroy

Gradle

repositories {
    maven { url "https://jitpack.io" }
}

AndroidX

dependencies {
    implementation 'com.github.wada811.LifecycleDisposable:lifecycledisposable:x.y.z'
}

Support Library

dependencies {
    implementation 'com.github.wada811.LifecycleDisposable:lifecycledisposablesupport:x.y.z'
}

GitHub