# Suspending over Views

DevFeed: [Suspending over Views](<https://devfeed.tech/articles/suspending-over-views-24927.md>)

Original publisher: [Read original article](<https://medium.com/androiddevelopers/suspending-over-views-19de9ebd7020?source=rss-9303277cb6db------2>)

Author: Chris Banes

Published: 2019-12-02T07:53:44Z

Content type: tutorial

Language: en

Sources: [Chris Banes on Medium](<https://devfeed.tech/sources/chris-banes-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/topics/kotlin-coroutines.md>), [Android](<https://devfeed.tech/topics/android.md>), [Jetpack](<https://devfeed.tech/topics/jetpack.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [blocking](<https://devfeed.tech/tags/blocking.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [performance](<https://devfeed.tech/tags/performance.md>), [recyclerview](<https://devfeed.tech/tags/recyclerview.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This article explains how Kotlin coroutines can simplify asynchronous Android view programming. It describes the callback-heavy Android view system, Jetpack KTX extension functions, and suspending functions that can model UI operations without blocking the main thread.

## Source excerpt

Illustration by Virginia PoltrackSuspending over viewsHow coroutines can make UI programming easier Kotlin Coroutines allow us to model asynchronous problems like synchronous code. That's great, but most usage seems to concentrate on I/O tasks and concurrent operations. Coroutines are great at modelling problems which work across threads, but can also model asynchronous problems on the same thread. There's one place which I think really benefits from this, and that's working with the Android view system. Android views 💘 callbacks The Android view system loves callbacks; like really loves callbacks. To give you an idea, there are currently 80+ callbacks in view and widgets classes in the Android framework, and then another 200+ in Jetpack (includes non-UI libraries, but you get the idea). Commonly used examples include: AnimatorListener to know when an animator finishes. RecyclerView.OnScrollListener to know when the scroll state changes. View.OnLayoutChangeListener to know when a view is laid out. Then there are the APIs which accept a Runnable to perform an async action, such as View.post() or View.postDelayed(), etc. There are so many callbacks because user interface programming on Android is inherently asynchronous. Everything from measure & layout, drawing, to inset dispatch are all performed asynchronously. Generally, something (usually a view) requests a traversal from the system, and then some time later the system dispatches the call, which then triggers any listeners. KTX extension functions For a lot of the APIs we've mentioned above, the team has added extension functions in Jetpack to improve the developer ergonomics. One of my favorites is View.doOnPreDraw(), which greatly simplifies waiting for the next draw to happen. There are many others which I use every day: View.doOnLayout() and Animator.doOnEnd() to name two. But these extension functions only go so far: they make a old-school callback API into a Kotlin-friendly lambda-based API. They're nicer t