I'm building a simple todo app. There is a single ...
# compose
c
I'm building a simple todo app. There is a single text field when you open the app and it should have the keyboard ready when the app opens. Is this the right way to do it?
Copy code
val focusRequester = FocusRequester()

  DisposableEffect(Unit) {
      focusRequester.requestFocus()
      onDispose { }
  }
  
OutlinedTextField(label = {Text("TODO")}, modifier = Modifier.focusRequester(focusRequester),
Or do I not need a sideEffect?
c
OOf. thats what i get for not scrolling up a bit. and here I thought I was following most of the conversations...
f
Usually everyone scrolls up to their question only 😁 I have mine not answered about 2D scrolling and i made 100 solutions that don't work but still hoping it'll be answered 🤷‍♂️
s
Colton did you get it to work without getting the exception I am showing here ? And if the solution is putting the DisposableEffect below the declaration of the focusRequester modifier, I wonder if this should be considered a hack and if it will even work if for whatever reason they run in opposite order.
c
No exceptions nope. My sample above works, I just wanted to know if that's the "right" way to do what I wanted to do. @Zach Klippenstein (he/him) [MOD] sorry for the direct mention, but I know you are working on text fields and focus now. Can you confirm if this is all you need to have a keyboard show up on launch?
Copy code
val focusRequester = FocusRequester()

  DisposableEffect(Unit) {
      focusRequester.requestFocus()
      onDispose { }
  }
  
OutlinedTextField(label = {Text("TODO")}, modifier = Modifier.focusRequester(focusRequester),
z
That should do it
🎉 2