The problem I’m having is that when I navigate from screen to screen using jetpack compose the talkback target does not move to the top of the new screen as it should, instead, it finds the closest target to the area of the screen as it was previously on. I.e if I tap a button to navigate in the centre of the screen, when the new screen loads it focuses on the centre of the screen and not the top. I’ve tried clear focus and focus requester but with no luck.
Here is the offending screen
@Composable
private fun IncidentScreen(
viewState: IncidentViewModel.ViewState,
onBackClicked: () -> Unit,
onMoreInfoClicked: () -> Unit,
onWebViewDismissed: () -> Unit,
) {
val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
focusManager.clearFocus(true)
focusRequester.freeFocus()
focusRequester.requestFocus()
}
BaseScreen(
title = viewState.incidentType,
navigationIcon = {
NavigationBackButton(onClick = onBackClicked, modifier = Modifier.focusTarget().focusRequester(focusRequester).focusable())
}
) { paddingValues ->
LazyColumn(Modifier.fillMaxWidth()) {
item {
MapPreview(
locationOnMap = viewState.locationOnMap,
severity = viewState.severity,
category = viewState.category
)
}
item {
IncidentHeroBar(
incidentType = viewState.incidentType,
incidentName = viewState.incidentName,
incidentDistance = viewState.distanceUser,
incidentDistanceFromWatchZone = viewState.distanceFromClosestWatchZone,
severity = viewState.severity,
category = viewState.category
)
}
items(viewState.incidentInfoItems) {
Box(
Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Column {
Spacer(modifier = Modifier.height(16.dp))
IncidentListItem(title = stringResource(id = it.title.title_res), body = it.body)
Spacer(modifier = Modifier.height(8.dp))
HorizontalDivider()
}
}
}
item {
SecondaryButton(
modifier = Modifier
.fillMaxWidth()
.padding(20.dp),
text = stringResource(id = R.string.btn_incident_more_info),
onClick = { onMoreInfoClicked.invoke() }
)
}
}
FadedVisibility(
visible = viewState.isLoading
) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.background(MaterialTheme.colorScheme.scrim.copy(alpha = 0.32f))
.padding(paddingValues)
.fillMaxSize()
) {
CircularProgressIndicator()
}
}
}
}```
I've already tried to use focus manager and focus requester with no luck
`
LaunchedEffect(Unit) {
focusManager.clearFocus(true)
focusRequester.freeFocus()
focusRequester.requestFocus()
}
`
Ben Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.