https://kotlinlang.org logo
Title
f

Fudge

07/03/2021, 5:08 PM
rememberSaveable
is supposed to persist a value across process death. However, I've tried killing the app and restarting it in multiple ways (even cases as simple as 'Live Edit of Literals'!), and every time the state is not persisted. Am I not understanding what
rememberSaveable
is actually supposed to do? A simple example:
var enabled by state { false }
                Button(onClick = { enabled = !enabled }) {
                    Text(text = if (enabled) "enabled" else "disabled")
                }
When the app is started,
enabled
should have the value it had when the app last closed, but that is not the case. It is not 'remembered' or 'saved', it is forgotten and lost.
l

louiscad

07/03/2021, 5:23 PM
If you kill the app, it will get discarded. It's only kept if the system kills the app (except for app upgrades or device restart). You can test by checking "Don't keep activities" in the developers options, and going to home screen and coming back. BTW, it's exactly the same as savedInstanceState throughout Android, so it's not really compose specific
☝️ 1
f

Fudge

07/03/2021, 5:29 PM
What should I use to persist state across user restarts then?
l

louiscad

07/03/2021, 5:29 PM
A database maybe?
f

Fudge

07/03/2021, 5:31 PM
Something like datastore? I guess that makes sense to make the app more consistent, as rememberSaveable uses some arbitrary compose hash that can change at any time.
l

louiscad

07/03/2021, 5:31 PM
That is, SqlDelight or Room would work great. androix.datastore is also an option for small amounts of data.
f

Fudge

07/03/2021, 5:32 PM
Does compose navigation persist the current screen across user restarts?
l

louiscad

07/03/2021, 5:32 PM
Only if the system killed the app
Relaunch after user swiping the app away from recents, or device restart, or app upgrade -> starting from scratch.
f

Fudge

07/03/2021, 5:36 PM
Thanks. That seems suboptimal in my opinion.
l

louiscad

07/03/2021, 5:38 PM
To me, that's the normal behavior. If you kill the app manually, you don't really want it to save the navigation state, you want it to start from whatever "scratch" means. If for your app, it makes sense to keep some navigation state, then make it "optimal", save it and do the tests.
👍 2
f

FunkyMuse

07/03/2021, 6:07 PM
To simulate process death, just click home button so that the app will go into recents, once you do that there's a stop button and that's how you do that, later on you launch it from recents.
a

Arkadii Ivanov

07/03/2021, 7:06 PM
I think it is important to discard the saved state if the app is killed by the user or crashed. Otherwise the app may stuck forever in an inappropriate state, crashing every time user launches it.
👍 1