Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 18, 2021. It is now read-only.

Useful extensions for coroutines. AutoDispose + MainScope

License

NotificationsYou must be signed in to change notification settings

bennyhuo/kotlin-coroutines-android

Repository files navigation

Useful extensions for coroutines.

Provide easier MainScope integration and auto disposable Job according to the view attached state.

AutoDisposable

Inspired byAutoDispose, auto cancel the job according to the attach state of corresponding view.

Usage

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

MainScope

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.

Usage

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.

About Fragment

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.

Issue

Please feel free to issue and pull request.

License

MIT License

About

Useful extensions for coroutines. AutoDispose + MainScope

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp