What do you want to do with it <@UC2ML87NF>? You m...
# compose
d
What do you want to do with it @Rahul Sainani? You might be looking for
remember
r
One usecase would be to signal the viewmodel to subscribe to some data or load initial data. But ideally would like to only do it once on visiting the "Screen" and not on coming back from a deeper level.
remember
doesn't work exactly like that but
rememberSaveable
with a flag like isFirstComposition does solve it but was searching for a recommended pattern, does that clarify the issue?
z
Look at
DisposableEffect
and
LaunchedEffect
r
Seems like LaunchedEffect gets called on coming back to the screen as well, example to help test.
Copy code
// part of SplashScreen
LaunchedEffect(Unit) {
        // Gets called on coming back from home as well
        navController.navigate(Route.home) {}
    }
z
Yes, it will get called the first time it enters the composition. So if it is skipped/leaves the composition, and then enters again, it will get called again. You could use
rememberSaveable
to set a flag the first time it’s ran, so that when it’s navigated back to the flag would be restored and you could avoid re-executing code.
👍 1
But I would probably argue this sort of navigation logic (“Only do this thing the first time this screen is navigated to”) belongs in your navigation/business logic layer, not the compose/view layer.
4
💯 1
r
Yeah you're right about the navigation logic, I wanted to use this to highlight as a visual example 🙂 rememberSaveable does work for sure! Thanks for the help! 🙌
👍 1