Lukasz Kalnik
02/17/2022, 3:42 PMTextField with prefilled text to set the cursor at the end of the text when it gains focus using LaunchedEffect { focusRequester.requestFocus() }.
I store the text string itself in a ViewModel, but I don't want to keep the whole TextFieldValue (including cursor position) inside the ViewModel.
It seems that the TextField should be able to take care of its cursor position on its own. Is there a way to do it?Lukasz Kalnik
02/17/2022, 3:49 PMremember inside the composable to store the TextFieldValue and storing the text string itself outside the composable, in ViewModel?Lukasz Kalnik
02/17/2022, 3:55 PMuiState.name variable) like this:
@Composable
fun EnterNameDialog(
uiState: EnterNameDialogState,
) {
val currentText = remember(uiState.name) { mutableStateOf(uiState.name) }
AlertDialog(
text = {
OutlinedTextField(
value = currentText.value,
onValueChange = { currentText.value = it },
// ...
}
But the problem is that after a recomposition the initial uiState.name gets restored (because it's remembered).
How can I update currentText.value so that it doesn't get overwritten after a recomposition? What's the point of having write access to a MutableState if it gets forgotten after a recomposition?
Is there a way to "reinitialize" remember? Or should I not use remember at all?Zoltan Demant
02/17/2022, 3:56 PMremember(uiState.name) which results in a new mutableState being created everytime the name changes. If you just do remember without any argument, it will retain the value for as long as the composable is active, and I think thats what you want.Lukasz Kalnik
02/17/2022, 3:59 PMLukasz Kalnik
02/17/2022, 4:00 PMcurrentText.value gets forgotten as soon as recomposition happensLukasz Kalnik
02/17/2022, 4:01 PMonValueChange = { currentText.value = it }
This updates the text as long as I type. The moment I e.g. change the device configuration and the recomposition happens, the old remembered state (with the uiState.name initially set from the ViewModel) gets restored.Lukasz Kalnik
02/17/2022, 4:02 PMremember ignores the last changes to the MutableStateLukasz Kalnik
02/17/2022, 4:05 PMMutableState without rememberZoltan Demant
02/17/2022, 4:06 PMrememberSaveable, that should persist the value across orientation changes, etc; just using remember will result in the value being recreated, since the scope of the composable is recreated when the device is rotated.Lukasz Kalnik
02/17/2022, 4:07 PMLukasz Kalnik
02/17/2022, 4:08 PMLukasz Kalnik
02/17/2022, 4:08 PMremember doesn't survive configuration change...Lukasz Kalnik
02/17/2022, 4:09 PMColton Idle
02/17/2022, 5:26 PMlesincs
02/18/2022, 3:32 AMColton Idle
02/18/2022, 5:31 AMLukasz Kalnik
02/18/2022, 8:57 AMArun Joseph
02/23/2022, 8:46 PMonValueChanged send it to the view model and letting view model update uiState.name? I have similar case and i do this .Lukasz Kalnik
02/24/2022, 8:53 AMremember) to set the cursor position at the end of the text when the dialog is open. There is no other way to set cursor position in TextField. And I don't want to keep the whole TextFieldValue including the cursor position inside the ViewModel. ViewModel should not know anything about the cursor position, unless there is some really custom logic based on this.Lukasz Kalnik
02/24/2022, 8:55 AMuiState.name in ViewModel. You need another TextField method overload which takes TextFieldValue as value (instead of String). TextFieldValue also contains the cursor position.Arun Joseph
02/24/2022, 8:58 AM