<https://stackoverflow.com/questions/76331958/how-...
# compose
s
I think the simplest way is to remember it in the ViewModel. Your VM should be created on A->B and reused on C->B.
c
Copy code
val resultViewModel = remember { viewModel }
like this?
s
No, with a property inside
ResultViewModel
.
Copy code
class ResultViewModel {
    ...
    var sentMessage = false
    fun sendMessageAPI(...) {
        if (!sentMessage) {
            ...
            sentMessage = true
        }
    }
}
p
I usually use a simple state machine with states Created and Started. Initial state is Created and it transitions to Started the first onStart call. At the transition do the stuff. Subsequent calls to onStart has no effect.
c
@Pablichjenkov Can you give me some example..?
@Sean Proctor Thank you, I will try it.
p
I will set up something for you.
Something like this: https://pl.kotl.in/9Gh1pv-GU
You will call ViewModel.start() from your Composable navbackstackentry Lifecycle event listener.
I find this way more elegant than using a
isFirstTimeCall
boolean flag or those types of solutions.
c
@Pablichjenkov Thank you but, I need to call other APIs when I come back from both sides, So, only Created doesn't work. I use Prepping -> Loading -> Idle states and when it's Prepping it won't have enough value for calling the API when it enters the Screen at first time. It should be Idle state so that it's make sure it has the resultData from the server. So, maybe having a flag would be better?
p
I see, well in that case you will need some sort of mark, either a flag or enum something. My solution is to execute one task only the first time onStart() is called. But you need more than that. In such a case you would have to use some kind of result value from the pop screen to determine what to do. Like Sean response above.