Gaining focus in Jetpack Compose and a small issue in Modifier API to be careful about.

Umair Khalid
2 min readFeb 13, 2023

--

Hello Jetpack composers,

This article, there will be two parts as described in the title. Examples explaining how to gain focus in jetpack compose and an issue in Modifier API to be careful

Here is the gist showing a basic autofocus-gaining example

PART 1 — How to request focus in jetpack compose

It will produce as expected results. Nice.

PART 2 — Problem in Modifier API

What if I tell you that if you somehow change the order of the two functions of the Modifier, it will stop working?

Never thought that I will spend two days finding the reason why I am not able to gain focus on a button.

How would you normally use a modifier API in jetpack compose?

You will start adding props by following the builder pattern, without considering the order of props. Probably in most cases, the answer will be yes.

// Correct 
.focusRequester(focusRequester)
.focusable(interactionSource = interactionSource)

// Incorrect
.focusable(interactionSource = interactionSource)
.focusRequester(focusRequester)

Conclusion
Always respect the order of function calling in modifier API.

Let me know if it helps you 😄

--

--