How to prevent execution's of action from pop backstack from previous screen in jetpack compose

Viewed 131

I have Two Screen A And Screen B

Screen A execute

sharedPrefs.setUserLatLng(CURRENT_LOCATION_LAT_KEY, CURRENT_LOCATION_LNG_KEY, LatLng(location.latitude, location.longitude))
commonViewModel.onLatLngChange(LatLng(location.latitude, location.longitude))

These Two Lines of code in Launch Effect

When a button click in Screen A It navigate to Screen B

Everything works fine.

When I Back press from Screen B.

Screen A execute above mention two lines of code. I want to prevent that execution when back press from Screen B

1 Answers

You can save a flag which will indicate if this side effect was already processed, like this:

var userLatLngUpdated by rememberSaveable { mutableStateOf(false) }
if (!userLatLngUpdated) {
    LaunchedEffect(Unit) {
        // your code 
        userLatLngUpdated = true
    }
}
Related