{"id":17688854,"url":"https://github.com/kizitonwose/android-disposebag","last_synced_at":"2025-04-13T18:40:32.445Z","repository":{"id":90072717,"uuid":"113319492","full_name":"kizitonwose/android-disposebag","owner":"kizitonwose","description":"Automatically dispose RxJava 2 streams on Android using Lifecycle events.","archived":false,"fork":false,"pushed_at":"2019-05-14T18:36:59.000Z","size":73,"stargazers_count":205,"open_issues_count":0,"forks_count":12,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-27T09:21:37.097Z","etag":null,"topics":["android","auto-dispose","disposebag","kotlin","lifecycle","rxjava","rxkotlin"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kizitonwose.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-06T13:24:25.000Z","updated_at":"2023-08-14T20:09:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"bb2892a9-0874-4be0-8520-b79c1b36f5ed","html_url":"https://github.com/kizitonwose/android-disposebag","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2Fandroid-disposebag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2Fandroid-disposebag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2Fandroid-disposebag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2Fandroid-disposebag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kizitonwose","download_url":"https://codeload.github.com/kizitonwose/android-disposebag/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248764197,"owners_count":21158040,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","auto-dispose","disposebag","kotlin","lifecycle","rxjava","rxkotlin"],"created_at":"2024-10-24T11:45:43.919Z","updated_at":"2025-04-13T18:40:32.423Z","avatar_url":"https://github.com/kizitonwose.png","language":"Kotlin","funding_links":[],"categories":["rxjava"],"sub_categories":[],"readme":"# Android-DisposeBag\n\n[![Build Status](https://travis-ci.org/kizitonwose/android-disposebag.svg?branch=master)](https://travis-ci.org/kizitonwose/android-disposebag) \n[![Coverage](https://img.shields.io/codecov/c/github/kizitonwose/android-disposebag/master.svg)](https://codecov.io/gh/kizitonwose/android-disposebag) \n[![JitPack](https://jitpack.io/v/kizitonwose/android-disposebag.svg)](https://jitpack.io/#kizitonwose/android-disposebag) \n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/kizitonwose/android-disposebag/blob/master/LICENSE.md) \n\n[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. \n\nThis library uses the new LifecycleObserver introduced in Android Architecture Components to automatically dispose [RxJava][rxjava-url]/[RxKotlin][rxkotlin-url] streams at the right time.\n\n## Usage\n\n### Using a DisposeBag\n\nCreate a DisposeBag, supply your LifecycleOwner, then add all your disposables.\n\nThe example below uses an Activity but this also works with Fragments or any other class that impements the LifecycleOwner interface.\n\n##### Kotlin:\n\n```kotlin\nclass MainActivity : AppCompatActivity() {\n    \n    val bag = DisposeBag(this)\n    \n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        \n        val button = findViewById\u003cButton\u003e(R.id.button)\n        \n        button.clicks()\n                .subscribe {\n                    // Handle button clicks\n                }.disposedBy(bag)\n    }\n}\n```\n\n##### Java:\n\n```java\npublic final class MainActivity extends AppCompatActivity {\n\n    final DisposeBag bag = new DisposeBag(this);\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        \n        final Button button = findViewById(R.id.button);\n\n        bag.add(RxView.clicks(button)\n                .subscribe(o -\u003e {\n                    // Handle button clicks \n                }));\n    }\n}\n```\n\nIn 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][rxbinding-url]. Internally, the DisposeBag uses a [CompositeDisposable][compositedisposable-java-url].\n\nYou can change the dipose event by specifying it when creating the DisposeBag:\n\n##### Kotlin:\n\n```kotlin\n// This is disposed at the \"on stop\" event\nval bag = DisposeBag(this, Lifecycle.Event.ON_STOP)\n```\n\n##### Java:\n\n```java\n// This is disposed at the \"on stop\" event\nDisposeBag bag = new DisposeBag(this, Lifecycle.Event.ON_STOP);\n```\n\n### Using a LifecycleOwner\n\nSince 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.\n\nThe example below uses a Fragment but of course you can use an Activity or any other LifeCycleOwner.\n\n```kotlin\nclass MainFragment : Fragment() {\n        \n    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {\n        super.onViewCreated(view, savedInstanceState)\n        \n        val button = view.findViewById\u003cButton\u003e(R.id.button)\n        \n        button.clicks()\n                .subscribe {\n                    // Handle button clicks\n                }.disposedWith(this)\n    }\n\n}\n```\n\nYou can also change the event which triggers the disposal, default is `Lifecycle.Event.ON_DESTROY`\n\n```kotlin\nbutton.clicks()\n        .subscribe {\n            // Handle button clicks\n        }.disposedWith(this, Lifecycle.Event.ON_STOP) // Change the dispose event\n```\n\n\u003e Note the difference between the two: `disposedBy()` and `disposedWith()`\n\n## Changing the default dispose event globally\n\nIf you would like to change the default dispose event, you can do this via the `DisposeBagPlugins`\n\nIn your app's Application class:\n\n##### Kotlin:\n\n```kotlin\nclass MyApp : Application() {\n\n    override fun onCreate() {\n        super.onCreate()\n        DisposeBagPlugins.defaultLifecycleDisposeEvent = Lifecycle.Event.ON_STOP\n    }\n\n}\n```\n\n##### Java:\n\n```java\npublic final class MyApp extends Application {\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        DisposeBagPlugins.setDefaultLifecycleDisposeEvent(Lifecycle.Event.ON_STOP);\n    }\n}\n```\n\nAnd from then, your app's default dispose event will be `Lifecycle.Event.ON_STOP` instead of `Lifecycle.Event.ON_DESTROY`\n\n## Installation\n\nAdd the JitPack repository to your `build.gradle`:\n\n```groovy\nallprojects {\n repositories {\n    maven { url \"https://jitpack.io\" }\n    }\n}\n```\n\nAdd the dependency to your `build.gradle`:\n\n```groovy\ndependencies {\n    implementation 'com.github.kizitonwose:android-disposebag:0.1.0'\n}\n```\n\n#### Note: \n\nIf 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: \n\n```groovy\nandroid {\n    compileOptions {\n        sourceCompatibility JavaVersion.VERSION_1_8\n        targetCompatibility JavaVersion.VERSION_1_8\n    }\n}\n```\n\n## Contributing\n\nThis library is a combination of some extension functions and classes I wrote in a project of mine, improvements are welcome.\n\n## License\n\nDistributed under the MIT license. [See LICENSE](https://github.com/kizitonwose/android-disposebag/blob/master/LICENSE.md) for details.\n\n[rxswift-url]: https://github.com/ReactiveX/RxSwift \n[disposebag-swift-url]: https://github.com/ReactiveX/RxSwift/blob/master/RxSwift/Disposables/DisposeBag.swift \n[rxjava-url]: https://github.com/ReactiveX/RxJava \n[rxkotlin-url]: https://github.com/ReactiveX/RxKotlin\n[rxbinding-url]: https://github.com/JakeWharton/RxBinding\n[compositedisposable-java-url]: https://github.com/ReactiveX/RxJava/blob/2.x/src/main/java/io/reactivex/disposables/CompositeDisposable.java\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkizitonwose%2Fandroid-disposebag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkizitonwose%2Fandroid-disposebag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkizitonwose%2Fandroid-disposebag/lists"}