In my app, the user can submit a form for a Person
, and later on they can edit that form. This brings up a screen with multiple TextField
s that are populated with the Person
's info. I'm having an issue where when I edit one form and then go to edit another, the TextField
s contain the info from the first Person
instead of the current Person
. This is how I'm implementing it:
@Composable
fun FormContent(
person: Person? // nullable because I'm retrieving from database and there could be an exception
) {
var name by rememberSaveable(person) { mutableStateOf(person?.name.orEmpty()) }
var age by rememberSaveable(person) { mutableStateOf(person?.age.orEmpty()) }
var gender by rememberSaveable(person) { mutableStateOf(npc?.gender.orEmpty()) }
// These are inside a Column
TextField(
label = stringResource(id = R.string.name),
onValueChangeAction = { name = it },
startingText = person?.name.orEmpty()
)
TextField(
label = stringResource(id = R.string.age),
onValueChangeAction = { age = it },
startingText = person?.age.orEmpty()
)
TextField(
label = stringResource(id = R.string.gender),
onValueChangeAction = { gender = it },
startingText = person?.gender.orEmpty()
)
}
I've added the current person
as an input for rememberSaveable
so that it gets reset when a new Person
is passed in. However, when the first composition occurs, fields are set to the previous Person
's info, but then the fields are set to the new Person
's info upon subsequent recompositions, yet the TextField
s aren't updating. I've debugged and added logging and can see the subsequent recompositions do have the updated Person
, but the startingText
for the TextFields
stays the same. This is what logcat prints:
Person(id=15, name=Bob, age=16, gender=Male) // Previous person
Person(id=16, name=Amy, age=20, gender=Female) // Current person
Person(id=16, name=Amy, age=20, gender=Female)
From my understanding the TextField
s would get recomposed when person
changes, but that doesn't seem to be the case. Can someone let me know what I'm doing wrong?