https://kotlinlang.org logo
#compose
Title
# compose
n

Nat Strangerweather

03/04/2022, 6:01 PM
I f I wanted to save the state of my state class so that I have it intact when I navigate back to that page, what would be my best bet: Datastore, Saver function, some other way? 🧵
Copy code
class MainPieceComposableState(
    enabled: Boolean,
    visible: Boolean,
    tileColor: Color,
) {
    var enabled by mutableStateOf(enabled)
    var visible by mutableStateOf(visible)
    var tileColor by mutableStateOf(tileColor)
}
What I mean is that I want to remember exactly in my Composable which Piece was visible/enabled etc and which wasn't.
I could also use a Room database but that might be overkill?
а

Александр Макаров

03/04/2022, 6:08 PM
As i think, the Datastore preferences should be enough for this case
n

Nat Strangerweather

03/04/2022, 6:10 PM
Thanks, will give it a try! 🙂
а

Александр Макаров

03/04/2022, 6:12 PM
By the way, are you using one NavHost or multiple navhost, i've an issue with handling navigateUp event with multiple ones
n

Nat Strangerweather

03/04/2022, 6:16 PM
Just NavHost, I followed a very basic tutorial...
а

Александр Макаров

03/04/2022, 6:17 PM
Thank you at all, maybe other colleagues have implemented such case as I
👍 1
a

Alex Vanyo

03/04/2022, 6:32 PM
For choosing between
DataStore
(or another on-disk persistence) versus
Saver
+
rememberSaveable
, what behavior do you want if the app is completely closed, and relaunched in a fresh state?
😊 1
n

Nat Strangerweather

03/04/2022, 6:33 PM
Yes, I need the state to be remembered if someone leaves the game for a few days and comes back to it.
so datastore makes more sense
👍 1
a

Alex Vanyo

03/04/2022, 6:37 PM
Agreed!
Saver
+
rememberSaveable
are equivalent to
savedInstanceState
, so if you want to persist state beyond an
Activity
being completed, you’ll need some sort of disk storage like
DataStore
🙌 2