GitPedia

Android disposebag

Automatically dispose RxJava 2 streams on Android using Lifecycle events.

From kizitonwose·Updated March 5, 2026·View on GitHub·

[RxSwift][rxswift-url] has an inbuilt [DisposeBag][disposebag-swift-url] 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. The project is written primarily in Kotlin, distributed under the MIT License license, first published in 2017. Key topics include: android, auto-dispose, disposebag, kotlin, lifecycle.

Latest release: 0.1.0
December 14, 2017View Changelog →

Android-DisposeBag

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.

Usage

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.

Kotlin:
kotlin
class MainActivity : AppCompatActivity() { val bag = DisposeBag(this) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val button = findViewById<Button>(R.id.button) button.clicks() .subscribe { // Handle button clicks }.disposedBy(bag) } }
Java:
java
public final class MainActivity extends AppCompatActivity {    final DisposeBag bag = new DisposeBag(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Button button = findViewById(R.id.button); bag.add(RxView.clicks(button) .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:

Kotlin:
kotlin
// This is disposed at the "on stop" event val bag = DisposeBag(this, Lifecycle.Event.ON_STOP)
Java:
java
// 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.

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

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

kotlin
button.clicks() .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:

Kotlin:
kotlin
class MyApp : Application() { override fun onCreate() { super.onCreate() DisposeBagPlugins.defaultLifecycleDisposeEvent = Lifecycle.Event.ON_STOP } }
Java:
java
public final class MyApp extends Application { @Override public void onCreate() { super.onCreate(); DisposeBagPlugins.setDefaultLifecycleDisposeEvent(Lifecycle.Event.ON_STOP); } }

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

Installation

Add the JitPack repository to your build.gradle:

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

Add the dependency to your build.gradle:

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

Note:

If you get the error: Default interface methods are only supported starting with Android N (--min-api 24) after installing this library, this means that you need to compile your project with JDK 8. You can do this by adding the compileOptions block in the android block of your app-level build.gradle file:

groovy
android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }

Contributing

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

License

Distributed under the MIT license. See LICENSE for details.

Contributors

Showing top 1 contributor by commit count.

View all contributors on GitHub →

This article is auto-generated from kizitonwose/android-disposebag via the GitHub API.Last fetched: 6/27/2026