Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219· AnthropicHugging Face LMArena Human Preference 140K

how to preserve TextField focused state in compose after screen rotation?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-no-thinking’s answer.
  1. User

    how to preserve TextField focused state in compose after screen rotation?

  2. Claude Assistant

    In Jetpack Compose, you can preserve the TextField focus state across configuration changes like screen rotation by using rememberSaveable instead of just remember. Here's how to implement it:

    @Composable
    fun MyTextField() {
        val focusRequester = remember { FocusRequester() }
        var text by rememberSaveable { mutableStateOf("") }
        var isFocused by rememberSaveable { mutableStateOf(false) }
    
        TextField(
            value = text,
            onValueChange = { text = it },
            modifier = Modifier
                .focusRequester(focusRequester)
                .onFocusChanged { isFocused = it.isFocused }
        )
    
        // Restore focus if it was focused before rotation
        LaunchedEffect(Unit) {
            if (isFocused) {
                focusRequester.requestFocus()
            }
        }
    }
    

    Would you like me to explain how this code works?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.