- Notifications
You must be signed in to change notification settings - Fork9
Useful extensions for coroutines. AutoDispose + MainScope
License
bennyhuo/kotlin-coroutines-android
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Useful extensions for coroutines.
Provide easier MainScope integration and auto disposable Job according to the view attached state.
Inspired byAutoDispose, auto cancel the job according to the attach state of corresponding view.
Add dependency in gradle:
api "com.bennyhuo.kotlin:coroutines-android-autodisposable:1.0"UseasAutoDisposable to convert aJob to anAutoDisposableJob. Then the job will be cancelled when the view is removed.
In the sample below,delay(1000) will not be executed.
val anotherButton=Button(this)parentView.addView(anotherButton,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)anotherButton.setOnClickListener {GlobalScope.launch(Dispatchers.Main) { log(1) parentView.removeView(anotherButton) delay(1000) log(2) }.asAutoDisposable(it)}
Versions seeChangeLog.
Supplement for kotlinx.coroutines, providing useful extensions for android Views and easier way to integrate.
An instance ofMainScope which useDispatchers.Main as its dispatcher will be bound to the lifecycle of the correspondingActivity orFragment. In other words, an instance ofMainScope will be created when an activity is created and cancelled when the activity is destroyed or the view of the fragment is destroyed.
Add dependency in gradle:
api 'com.bennyhuo.kotlin:coroutines-android-mainscope:1.0'Initialize this library in your customizedApplication:
classApp :Application() {overridefunonCreate() {super.onCreate()MainScope.setUp(this) }}
ImplementBasicScoped to includeView extensions for android sdk,RecyclerViewScoped forRecyclerView,DesignScoped for android support design library,AppCompatScoped for android support appcompat library. If more than one interfaces are used, just implement whatever you need.
classMainActivity :AppCompatActivity(), AppCompatScoped, RecyclerViewScoped {...}
To access theMainScope, just use the propertymainScope. Be careful that it is not thread safe to use outsize the main thread.
classMainActivity :AppCompatActivity(), BasicScoped {overridefunonCreate(savedInstanceState:Bundle?) {... mainScope.launch { log("Hey!") }... }}
It is also easy to launch multi-coroutines with thewithMainScope method:
...withMainScope { launch {... } async(Dispatchers.IO) {... }}...
Most of the listeners borrowed fromAnko are equipped withMainScope instead of theGlobalScope so that coroutines launched in these listeners will be cancelled when the corresponding activity is destroyed.
... button.onClick { log(1) delay(1000) log(2) textView.text="Hello Coroutine!" }...
onClick receives a suspend lambda as the body of the job. Once the button clicked, the lambda block will be started immediately on the main thread, and suspended atdelay(1000). If you leave the activity by pressing the back key, the suspend lambda won't be dispatched since the job is cancelled by theMainScope installed in the activity.
Sinceandroid.app.Fragment is deprecated, we choose to supportandroid.support.v4.app.Fragment only. If you include the library into the classpath, Fragment support will be automatically enabled for subtypes ofFragmentActivity. Otherwise if you never use Fragment, don't worry, nothing will happen.
FragmentLifecycleCallbacks was added from v25.1.0 so older versions will not be supported.
Please feel free to issue and pull request.
About
Useful extensions for coroutines. AutoDispose + MainScope
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.