Skip to content

Latest commit

 

History

History
executable file
·
194 lines (133 loc) · 6.22 KB

README.md

File metadata and controls

executable file
·
194 lines (133 loc) · 6.22 KB

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

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

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:
class MyApp : Application() {

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

}
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:

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

Add the dependency to your build.gradle:

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:

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.