Automatically dispose RxJava 2 streams on Android using Lifecycle events.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore Ignore idea folder Dec 12, 2017
gradlew Initial commit. Nov 29, 2017
settings.gradle Initial commit. Nov 29, 2017


Build Status Coverage JitPack License

RxSwift has an inbuilt DisposeBag container which disposes all disposables when it is deinitialized. Unfortunately, there is no reliable way to achieve the same result in Java/Kotlin. Even if we could achieve this, there's still a problem with the Android platform, Activities are created and managed by the system, using it after either onDestroy or onStop method is called will result to a crash.

This library uses the new LifecycleObserver introduced in Android Architecture Components to automatically dispose RxJava/RxKotlin streams at the right time.


Using a DisposeBag

Create a DisposeBag, supply your LifecycleOwner, then add all your disposables.

The example below uses an Activity but this also works with Fragments or any other class that impements the LifecycleOwner interface.

class MainActivity : AppCompatActivity() {
    val bag = DisposeBag(this)
    override fun onCreate(savedInstanceState: Bundle?) {
        val button = findViewById<Button>(
                .subscribe {
                    // Handle button clicks
public final class MainActivity extends AppCompatActivity {

    final DisposeBag bag = new DisposeBag(this);

    protected void onCreate(Bundle savedInstanceState) {
        final Button button = findViewById(;

                .subscribe(o -> {
                    // Handle button clicks 

In the examples above, the DisposeBag automatically disposes all disposables when the activity is destroyed. The clicks() extension function and RxView.clicks() static method are both from RxBinding. Internally, the DisposeBag uses a CompositeDisposable.

You can change the dipose event by specifying it when creating the DisposeBag:

// This is disposed at the "on stop" event
val bag = DisposeBag(this, Lifecycle.Event.ON_STOP)
// This is disposed at the "on stop" event
DisposeBag bag = new DisposeBag(this, Lifecycle.Event.ON_STOP);

Using a LifecycleOwner

Since the DisposeBag basically just acts on lifecycle events, you can directly use the LifecycleOwner to dispose your disposables without having to first create the DisposeBag.

The example below uses a Fragment but of course you can use an Activity or any other LifeCycleOwner.

class MainFragment : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val button = view.findViewById<Button>(
                .subscribe {
                    // Handle button clicks


You can also change the event which triggers the disposal, default is Lifecycle.Event.ON_DESTROY

        .subscribe {
            // Handle button clicks
        }.disposedWith(this, Lifecycle.Event.ON_STOP) // Change the dispose event

Note the difference between the two: disposedBy() and disposedWith()

Changing the default dispose event globally

If you would like to change the default dispose event, you can do this via the DisposeBagPlugins

In your app's Application class:

class MyApp : Application() {

    override fun onCreate() {
        DisposeBagPlugins.defaultLifecycleDisposeEvent = Lifecycle.Event.ON_STOP

public final class MyApp extends Application {

    public void onCreate() {

And from then, your app's default dispose event will be Lifecycle.Event.ON_STOP instead of Lifecycle.Event.ON_DESTROY


Add the JitPack repository to your build.gradle:

allprojects {
 repositories {
    maven { url "" }

Add the dependency to your build.gradle:

dependencies {
    implementation 'com.github.kizitonwose:android-disposebag:0.1.0'


This library is a combination of some extension functions and classes I wrote in a project of mine, improvements are welcome.


Distributed under the MIT license. See LICENSE for details.