# OkLayoutInflater: Improving Android XML Layout Inflation with Parallel Coroutines

DevFeed: [OkLayoutInflater: Improving Android XML Layout Inflation with Parallel Coroutines](<https://devfeed.tech/articles/oklayoutinflater-37405.md>)

Original publisher: [Read original article](<https://medium.com/okcredit/oklayoutinflater-3c5cd93c6ebc?source=rss----40ea5327aac7---4>)

Author: Anjal Saneen

Published: 2022-10-26T01:27:02Z

Content type: tutorial

Language: en

Sources: [OkCredit - Medium](<https://devfeed.tech/sources/okcredit-medium.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [XML](<https://devfeed.tech/topics/xml.md>), [Code](<https://devfeed.tech/topics/code.md>), [Parsing](<https://devfeed.tech/topics/parsing.md>), [implementation](<https://devfeed.tech/topics/implementation.md>), [IO](<https://devfeed.tech/topics/io.md>), [callback](<https://devfeed.tech/topics/callback.md>), [interface](<https://devfeed.tech/topics/interface.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [anr](<https://devfeed.tech/tags/anr.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [file](<https://devfeed.tech/tags/file.md>), [io](<https://devfeed.tech/tags/io.md>), [main-thread](<https://devfeed.tech/tags/main-thread.md>), [memory](<https://devfeed.tech/tags/memory.md>), [mobile-app-development](<https://devfeed.tech/tags/mobile-app-development.md>), [parallelism](<https://devfeed.tech/tags/parallelism.md>), [parsing](<https://devfeed.tech/tags/parsing.md>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This tutorial explains how OkLayoutInflater addresses limitations in AndroidX AsyncLayoutInflater. It describes the existing implementation's sequential background inflation and presents a coroutine-based approach intended to use parallelism based on CPU cores, helping recycler view items load and scroll faster.

## Source excerpt

The loading of large XML layouts is one of the major performance bottlenecks in android. Loading XML layout into memory through IO operations and parsing views through reflection can be expensive. This is especially true when the XML file is too large or when initializing the layout view takes a while. We know that when the main thread performs some time-consuming operations, it may cause the page to freeze, and even more, serious ANR may occur. Androidx AsyncLayoutInflater helps to load those layouts asynchronously. However, we found that it has some limitations. The goal of this blog post is to explain how OkLayoutInflater addresses these limitations. Source code analysis of AndroidX AsyncLayoutInflater https://android.googlesource.com/platform/frameworks/support/+/89f7eba/v4/java/android/support/v4/view/AsyncLayoutInflater.java Source code is short and easy to understand with only a few lines of code. In the inflate method, it creates an InflateRequest object and stores variables such as resid, parent, callback, etc. Then it calls enqueue to add the request to the InflateThread queue. The main purpose of InflateThread is to add requests to the blocking queue and perform BasicInflater.inflate operations in order. Regardless of inflating success or failure, the request message will be sent to the main thread for processing. The BasicInflater Inherited from LayoutInflater. In OnCreateView these prefixes are loaded on the layout. At last, mHandlerCallback implements handleMessage which executes the operation on main thread, and there is a fallback mechanism, that is when the child thread inflates fails. it will continue to inflate on the main thread, and finally, gives a callback to the main thread through the OnInflateFinishedListener interface. The limitations of AsyncLayoutInflater and how we improve it Single thread to do all the inflate work AsyncLayoutInflater has this major limitation. This was the main reason for creating a custom implementation. One of our r