2

I have a composable like here.

@Composable
fun MyBasicTextField() {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusRequester = remember{ FocusRequester() }

    BasicTextField(
        modifier = Modifier
            .focusRequester(focusRequester),
        keyboardActions = keyboardActions ?: KeyboardActions(onAny = { keyboardController?.hide() }),

    )

    LaunchedEffect(Unit) {
        Log.d(TAG, "focusRequester.requestFocus()")
        focusRequester.requestFocus()
    }
}
  • When the screen where this composable is used opens for the first time, always the keyboard is shown (above log message is visible).
  • Then I leave the app at that state and open another app (opening a link on that screen, which opens the default browser for instance)
  • Tapping on the BACK button (triangle) leaved the other app (e.g. webbrowser) and comes back to the initial screen: Either with opened Android keyboard on some devices or without any keyboard showing.

I have the feeling that the screen did not notice the missing keyboard (which disappreared while leaving the app for the browser) and thus does not recompose anything?

Can I flag to compose the screen from fresh everytime I re-/compose it?

Edna Krabappel
  • 362
  • 1
  • 2
  • 16

1 Answers1

0

I'm not sure why you need this, because focusing and showing keyboard every time re-composition happens may be undesirable, what if the user is doing something else on the screen which updates the field, you don't want want to show the keyboard but re-composition happens and you pop it again. Having said this it's of course doable: For example the argument passed into the LaunchedEffect determines if during re-composition the block will be run. So you could try something like this:

@Composable
fun MyBasicTextField() {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusRequester = remember{ FocusRequester() }
    var launchKey by remember { mutableStateOf(0) }
    
    BasicTextField(
        modifier = Modifier
            .focusRequester(focusRequester),
        keyboardActions = keyboardActions ?: KeyboardActions(onAny = { keyboardController?.hide() }),

    )
    //passing launchKey instead of Unit
    LaunchedEffect(launchKey++) {
        Log.d(TAG, "focusRequester.requestFocus()")
        focusRequester.requestFocus()
    }
}
David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39