All Projects → kizitonwose → Android Disposebag

kizitonwose / Android Disposebag

Licence: mit
Automatically dispose RxJava 2 streams on Android using Lifecycle events.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Android Disposebag

Star Wars Shop
Simple project with clean architecture and android lifecycle
Stars: ✭ 37 (-81.5%)
Mutual labels:  rxjava, rxkotlin, lifecycle
Net
Android上强大的网络请求
Stars: ✭ 344 (+72%)
Mutual labels:  rxjava, rxkotlin, lifecycle
Mango
🏀 An Android app for dribbble.com
Stars: ✭ 659 (+229.5%)
Mutual labels:  rxjava, rxkotlin
Mvvmhabit
goldze: 本人喜欢尝试新的技术,以后发现有好用的东西,我将会在企业项目中实战,没有问题了就会把它引入到MVVMHabit中,一直维护着这套框架,谢谢各位朋友的支持。如果觉得这套框架不错的话,麻烦点个 star,你的支持则是我前进的动力!
Stars: ✭ 6,789 (+3294.5%)
Mutual labels:  rxjava, lifecycle
S Mvp
🔥🔥优化版MVP,使用注解泛型简化代码编写,使用模块化协议方便维护,APT过程使用注解解析器利用JavaPoet🌝完成重复模块的编写,利用ASpect+GradlePlugin 完成横向AOP编程+Javassist动态字节码注入+Tinker实现热修复+Retrofit实现优雅网络操作+RxJava轻松玩转数据处理
Stars: ✭ 1,095 (+447.5%)
Mutual labels:  rxjava, lifecycle
Android Jetpack Demo
🔥 快速入门Android Jetpack以及相关Kotlin、RxJava、MVVM等主流技术,独立构架App的基础技能
Stars: ✭ 335 (+67.5%)
Mutual labels:  rxjava, lifecycle
Weatherapp
5 Day Forecast app that works on Android and uses latest tools (Kotlin, Navigation, Room, LiveData, Databinding, Dagger 2)
Stars: ✭ 426 (+113%)
Mutual labels:  rxjava, rxkotlin
Mvvmhabitcomponent
👕基于MVVMHabit框架,结合阿里ARouter打造的一套Android MVVM组件化开发方案
Stars: ✭ 857 (+328.5%)
Mutual labels:  rxjava, lifecycle
Rxkprefs
🛠 A small Kotlin library to make shared preferences easy + RxJava and Coroutines support
Stars: ✭ 264 (+32%)
Mutual labels:  rxjava, rxkotlin
Kotlinrxmvparchitecture
Clean MVP Architecture with RxJava + Dagger2 + Retrofit2 + Mockito + Fresco + EasiestGenericRecyclerAdapter using Kotlin. Includes Unit Tests(Kotlin Tests)!
Stars: ✭ 94 (-53%)
Mutual labels:  rxjava, rxkotlin
Androcat
AndroCat is a GitHub client for Android phones and provides to user GitHub user interface like how they used to.
Stars: ✭ 87 (-56.5%)
Mutual labels:  rxjava, rxkotlin
Mvvm Architecture
The practice of MVVM + Jetpack architecture in Android.
Stars: ✭ 1,634 (+717%)
Mutual labels:  rxjava, lifecycle
Autodispose
Automatic binding+disposal of RxJava streams.
Stars: ✭ 3,209 (+1504.5%)
Mutual labels:  rxjava, lifecycle
Rxbiometric
☝️ RxJava and RxKotlin bindings for Biometric Prompt (Fingerprint Scanner) on Android
Stars: ✭ 295 (+47.5%)
Mutual labels:  rxjava, rxkotlin
Kotlin Cleanarchitecture
This is a sample app that is part of a blog post I have written about how to architect android application using the Uncle Bob's clean architecture and Fernando Cejas Android-CleanArchitecture in Kotlin. Post in Spanish: http://katade.com/clean-architecture-kotlin/
Stars: ✭ 274 (+37%)
Mutual labels:  rxjava, rxkotlin
Doubanbook
一个基于 Clean 架构以及 Retrofit , RxKotlin , Dagger 框架实现的 Kotlin for Android App 。
Stars: ✭ 151 (-24.5%)
Mutual labels:  rxjava, rxkotlin
AndroidGo
Android、Flutter 开发者帮助 APP。包含事件分发、性能分析、Google Jetpack组件、OkHttp、RxJava、Retrofit、Volley、Canvas绘制以及优秀博文代码案例等内容,帮助开发者快速上手!
Stars: ✭ 30 (-85%)
Mutual labels:  rxjava, lifecycle
modern-android
Modern Android Project Skeleton
Stars: ✭ 17 (-91.5%)
Mutual labels:  rxjava, rxkotlin
Rxkotlin Rxjava2 Android Samples
Learning RxKotlin2 for Android by Examples - Migration From RxKotlin1/RxJava1 to RxKotlin2/RxJava2 - How to use RxKotlin 2 in Android - RxLogin using RxBinding - Pagination using RxKotlin
Stars: ✭ 65 (-67.5%)
Mutual labels:  rxjava, rxkotlin
Rxlifecycle
Rx binding of stock Android Activities & Fragment Lifecycle, avoiding memory leak
Stars: ✭ 131 (-34.5%)
Mutual labels:  rxjava, lifecycle

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].