3

Let's say I've the following code:

@Composable
fun Widget() {
    var text1 by remember { mutableStateOf("DEFAULT") }
    
    val picker = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.GetMultipleContents(),
        onResult = {
            for (uri in it) print(uri)
        },
    )
    
    Text(
        text = text1,
    )
    Button(
        onClick = { text1 = "CHANGED" },
    ) {
        Text(
            text = "Change text1",
        )
    }
    Button(
        onClick = { picker.launch("image/*") },
    ) {
        Text(
            text = "Launch Picker",
        )
    }
}

When my application is installed and launched for the first time if you change the text1 by pressing on the button labeled 'Change text1' and then press the button to launch the picker the composable state is lost and text1 reverts to "DEFAULT".

What is interesting is this happens only for the first time after installing and launching the app or after restarting the phone and using the app for the first time.

I also like to point out that this happens in both debug and release versions of the app.

So, I would like to know what could be the cause of this? is this a known compose bug? or is it how I instantiate/use the picker?

2 Answers 2

3

I think there might be a configuration change happening just the first time, which remember does not handle.

Try replacing remember with rememberSaveable. From docs:

While remember helps you retain state across recompositions, the state is not retained across configuration changes. For this, you must use rememberSaveable. rememberSaveable automatically saves any value that can be saved in a Bundle. For other values, you can pass in a custom saver object.

4
  • That indeed fixed the issue! but I can't remember complex types (like a Bitmap list for instance). I wonder what config change happens and causes the issue in the first place. Anyways, thank you a lot for your answer! Jan 2, 2022 at 21:10
  • 1
    @AbdelfattahRadwan Correct, you can only save Parcelable types. You could convert a Bitmap into a byte array and serialize that or you can use a view model, keep the complex types there. View models surive configuration changes. The only limitation there would be application death, where you would still need to serialize Bitmaps if a "saved state" restart is your goal.
    – Ma3x
    Jan 2, 2022 at 22:49
  • 2
    rememberSaveable() is not just for configuration changes. It is there for short-term process termination scenarios. Presumably, Abdelfattah's app's process is being terminated while its UI is in the background, after starting the GetMultipleContents system UI. So, rememberSaveable() is still a fine solution. Jan 4, 2022 at 12:50
  • 1
    @CommonsWare thanks for the info and for bringing my attention to the termination part. I have already switched to using ViewModels as Ma3x suggested and everything has been going fine since then. Jan 4, 2022 at 14:12
0

As the other answer already explains, remember is not enough to keep the object between configuration changes.

If you need to keep a complex object you can use rememberScoped with this library: https://github.com/sebaslogen/resaca

This is an alternative to rememberSaveable so you don't need to implement Parcelable nor Saver interfaces, but it won't keep the object between process deaths.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.