I have a screen with TextField and, ideally, I want to achieve the following behavior:
TextFieldshould become focused as soon as the screen is shown and the software keyboard must appear- User can clear the focus and hide the software keyboard (or I can do it programatically)
- When device is rotated, focus and the keyboard must retain their state (so, for example, if the keyboard was shown, it should stay)
Currently I can't even get keyboard to always show up after screen rotation.
I have the following code, but it does this: for example, if the screen was initially opened in portrait mode, when rotated into landscape the keyboard hides, but when rotated back to portrait it returns back. Focus always stays as expected.
val focusRequester = remember { FocusRequester() }
TextField(
value = ...,
onValueChange = { ... },
modifier = Modifier.focusRequester(focusRequester)
)
focusRequester.requestFocus()
Modifying it as follows works, but I don't really like this delay:
...
val keyboard = LocalSoftwareKeyboardController.current
LaunchedEffect(Unit) {
focusRequester.requestFocus()
delay(1000L) // Just a magic constant which works on my device
keyboard?.show()
}
How can I achieve the described behavior? I see that Google Search screen acts almost like I want, though, it doesn't remember keyboard's state and always opens it after rotation.