1

I have a MyBasicTextField in a composable to request user input:

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

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

    )

    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }
}

The keyboard automatically slides in when showing this composable, always.

But wherever MyBasicTextField is used:

  • I tap on a LinkifiedText to leave and open a browser to show link
  • I tap BACK
  • and come back to previous MyBasicTextField screen, the keyboard is not shown
  • also the focusRequester.requestFocus() is not triggered again when coming back

How can I solve my issue?

Edna Krabappel
  • 362
  • 1
  • 2
  • 16

2 Answers2

0

Create a top-level variable in your activity, then modify it from within the onStart overridden method. Use that variable as the key for LaunchedEffect in place of Unit. That variable basically keeps track of when the user enters the app.

var userIn by mutableStateOf (true)

In your Composable,

Launched effect(userIn){
  if(userIn && isKeyboardShown){
    ...
  }
}

Boring,

Richard Onslow Roper
  • 5,477
  • 2
  • 11
  • 42
0

You can use my answer here as well, but instead of incrmenting the launchKey every time it's called, only increment the launchKey once user clicks on the browser link, that way it will not pop up during other re-compositions.

David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39