Pablo
02/26/2024, 2:06 PMvar currentDessertImageId by rememberSaveable *{*
_mutableStateOf_(desserts[uiState.value.currentDessertIndex].imageId)
}
Something is not working because each recomposition, currentDessertImageId is not changing... is always the same, and that is not normal because each recomposition currentDessertIndex is different, and desserts[uiState.value.currentDessertIndex].imageId is different too, I checked it with a Log.
This is the Log:
2024-02-26 15:11:21.014 6790-6790 XXXX com.example.dessertclicker D currentDessertIndex: 0 + desserts[uiState.value.currentDessertIndex].imageId: 2130968578 + currentDessertImageId: 2130968578
2024-02-26 15:11:23.098 6790-6790 XXXX com.example.dessertclicker D currentDessertIndex: 1 + desserts[uiState.value.currentDessertIndex].imageId: 2130968579 + currentDessertImageId: 2130968578
2024-02-26 15:11:23.248 6790-6790 XXXX com.example.dessertclicker D currentDessertIndex: 2 + desserts[uiState.value.currentDessertIndex].imageId: 2130968580 + currentDessertImageId: 2130968578Chrimaeon
02/26/2024, 2:11 PMcurrentDessertIndex as an input parameter to remeberSavable otherwise, as the name suggests, its “remembered”.Pablo
02/26/2024, 2:14 PMcurrentDessertIndex should behave different than passing imageIdPablo
02/26/2024, 2:15 PMPablo
02/26/2024, 2:15 PMChrimaeon
02/26/2024, 2:17 PMval currentIndex = uiState.value.currentDessertIndex
var currentDessertImageId by rememberSaveable(currentIndex) {
mutableStateOf(desserts[currentIndex].imageId)
}
without the “input” parameter the `remember`’s closure is only called once with the first value. remeberX does not know that you “input” has changed and need to re-evaluate.Chrimaeon
02/26/2024, 2:18 PMdesserts change over time, you’ll need to pass that as well.Chrimaeon
02/26/2024, 2:20 PMChrimaeon
02/26/2024, 2:20 PMstores the value until it leaves the Composition. However, there is a way to invalidate the cached value. TherememberAPI also takes arememberorkeyparameter. If any of these keys change, the next time the function recomposes,keysinvalidates the cache and executes the calculation lambda block again. This mechanism gives you control over the lifetime of an object in the Composition. The calculation remains valid until the inputs change, instead of until the remembered value leaves the Composition.remember
Pablo
02/26/2024, 2:24 PMPablo
02/26/2024, 2:25 PMval currentDessertImageId = desserts[uiState.value.currentDessertIndex].imageIdPablo
02/26/2024, 2:26 PMPablo
02/26/2024, 2:26 PMPablo
02/26/2024, 2:26 PMPablo
02/26/2024, 2:27 PMPablo
02/26/2024, 2:27 PMPablo
02/26/2024, 2:28 PM