Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rememberLauncherForActivityResult causes composable to lose state?

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?

like image 951
Abdelfattah Radwan Avatar asked Oct 27 '25 10:10

Abdelfattah Radwan


1 Answers

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.

like image 82
Ma3x Avatar answered Oct 28 '25 23:10

Ma3x



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!