I am having a similar issue as this user, where `r...
# compose
d
I am having a similar issue as this user, where
requestFocus()
only works the first time it is called. Does anyone know how to work around this issue? https://stackoverflow.com/questions/74391260/jetpack-compose-requestfocus-works-only-once
I think the issue for me is that the composable is not recomposed if the error message is the same - so requestFocus doesn't get called again
z
Which version of compose?
d
Copy code
'1.3.2'
z
That link seems broken
Can you post your code? Focus doesn’t have anything to do with recomposition inherently, so I’d need to see exactly what you’re doing.
d
sorry, the repo was private - it's now public
its an entire project
I can post the code here
Copy code
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {

            var error = remember { mutableStateOf("") }
            var isError = remember { mutableStateOf(false) }
            var value = remember { mutableStateOf(TextFieldValue("")) }
            var focusRequester = remember { FocusRequester() }

            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Column() {
                        OutlinedTextField(
                            value.value,
                            modifier = Modifier.semantics {
                                if (isError.value) liveRegion = LiveRegionMode.Polite
                            }.focusRequester(focusRequester),
                            onValueChange = {
                                            value.value = it
                            },
                            isError = isError.value,
                            supportingText = {
                                if (isError.value) Text(text = error.value) else null
                            })
                        Button({
                            isError.value = !isError.value
                            error.value = if (isError.value) "we got errorz" else ""
                        }) {
                            Text("Button")
                        }
                    }
                }


                    if (isError.value) focusRequester.requestFocus()

            }
        }
    }
}
z
It looks like you’re calling requestFocus in the composition directly, which is not good. I think - slack makes code blocks impossible to read on phones 🙄
That should be in a DisposableEffect or a LaunchedEffect keyed off the value