Hey :wave: Kinda noob question, but I am a bit con...
# compose
o
Hey 👋 Kinda noob question, but I am a bit confused about this one. Why does
rememberSaveable
could store a list of instances, but not an instance alone? Code in the 🧵
This correctly crashes immediately on “Model cannot be saved using the current SaveableStateRegistry”
Copy code
data class Model(val id: String)

private fun randomModel(): Model = Model(UUID.randomUUID().toString())

@Composable
private fun Test() {
    val model01 = rememberSaveable { randomModel() }
    Text(model01.id)
}
But this runs perfectly fine, even shows the same UUID after rotation, thus really saving a Model in the Bundle 🤯
Copy code
@Composable
private fun Test() {
    val model02 = rememberSaveable { listOf(randomModel()) }
    Text(model02.first().id)
}
c
have you tried testing this with the do not keep activities dev setting? I think android does a lot of shenanigans to only do heavy work (like parcelisation) when it's absolutely required. So simply rotating the device may not see that happen.
o
Yeap, works the same with “do not keep” 😕
Hey, but with “do not keep” app silently crashes when I exit to the launcher 🤔
Copy code
Parcel: unknown type for value Model(id=33e40e5b-ad92-4dfa-a80a-e42a27e9d3f5)
So yea, you are right, but why the hell it optimises that rotation 😅
c
well, if you imagine, the whole parcelisation stuff is a mechanism that can survive process boundaries (i.e. your application closes and is no longer running), but when you rotate, it doesn't make a huge amount of sense to parcelise the data when you're just going to pull it straight out milliseconds later. So a nice optimisation is to skip parcelising. So unwitting developers like you and me think we've coped with rotation etc, release to live and then see that the Samsung Galaxy S-10 crashes onResume because it can't unparcel something
(p.s. I learnt about this in live on an app with over 100k downloads)
o
That’s a til moment for me, thx 😄