Useful extensions for coroutines.
Provide easier MainScope integration and auto disposable Job according to the view attached state.
Inspired by AutoDispose, 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"
Use asAutoDisposable
to convert a Job
to an AutoDisposableJob
. 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 see ChangeLog.
Supplement for kotlinx.coroutines, providing useful extensions for android Views and easier way to integrate.
An instance of MainScope
which use Dispatchers.Main
as its dispatcher will be bound to the lifecycle of the corresponding Activity
or Fragment
. In other words, an instance of MainScope
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 customized Application
:
class App : Application() {
override fun onCreate() {
super.onCreate()
MainScope.setUp(this)
}
}
Implement BasicScoped
to include View
extensions for android sdk, RecyclerViewScoped
for RecyclerView
, DesignScoped
for android support design library, AppCompatScoped
for android support appcompat library. If more than one interfaces are used, just implement whatever you need.
class MainActivity : AppCompatActivity(), AppCompatScoped, RecyclerViewScoped {
...
}
To access the MainScope
, just use the property mainScope
. Be careful that it is not thread safe to use outsize the main thread.
class MainActivity : AppCompatActivity(), BasicScoped {
override fun onCreate(savedInstanceState: Bundle?) {
...
mainScope.launch {
log("Hey!")
}
...
}
}
It is also easy to launch multi-coroutines with the withMainScope
method:
...
withMainScope {
launch {
...
}
async(Dispatchers.IO) {
...
}
}
...
Most of the listeners borrowed from Anko are equipped with MainScope
instead of the GlobalScope
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 at delay(1000)
. If you leave the activity by pressing the back key, the suspend lambda won't be dispatched since the job is cancelled by the MainScope
installed in the activity.
Since android.app.Fragment
is deprecated, we choose to support android.support.v4.app.Fragment
only. If you include the library into the classpath, Fragment support will be automatically enabled for subtypes of FragmentActivity
. 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.