https://kotlinlang.org logo
#compose
Title
# compose
h

Hasan Nagizade

03/09/2023, 6:19 PM
why onStart is getting triggered when navigating back to a composable and how can I avoid that?
p

Pablichjenkov

03/09/2023, 6:43 PM
Anytime a lifecycleowner is brought up, it's onstart gets called. Navbackstackentry is a lifecycle owner. Is by design. I don't think you can change it but you can build logic around it, to ignore the cases when you don't need to execute certain code when this function gets invoked
s

Stylianos Gakis

03/09/2023, 6:51 PM
The real question is what are you doing in reaction to this event which you don't want to happen in this case?
h

Hasan Nagizade

03/09/2023, 6:57 PM
@Pablichjenkov is there a way then to ignore certain page? for example don't trigger onStart if I'm coming from composable B.
@Stylianos Gakis I have chat application and every time I navigate back to the main composable I am losing scroll state. On start detection is needed to restart everything when user opens app from recents but at the same time I need to ignore this for just one page.
s

Stylianos Gakis

03/09/2023, 7:31 PM
For starters, conditionally doing something on start depending on if you've gone back into it or not sounds like a very tricky thing to do, maybe possible to avoid it. Why is the scroll state not restored, what kind of navigation are you using, and what exactly are you doing in this onStart?
p

Pablichjenkov

03/09/2023, 10:08 PM
is there a way then to ignore certain page? for >example don't trigger onStart if I'm coming from >composable B.
I am not aware of a specific way to ignore it. You can do things like having a flag onstartcallsCounter and increase it every time is called. Then something like if(counter == 1) {initialize} else {ignore}
Like Stylianos says gets very tricky but is the only thing that comes to my mind
h

Hasan Nagizade

03/10/2023, 6:10 AM
@Stylianos Gakis I am calling api to get data in onstart call. That's why scroll state is restored. I want to avoid that
s

Stylianos Gakis

03/10/2023, 7:00 AM
So you are doing a network request from inside your composable? Btw no need to ping me again when I'm already part of the discussion, I get the notifications
h

Hasan Nagizade

03/10/2023, 7:04 AM
Yes and no. I am using DisposableEffect to detect onStart inside composable. If that happens I am reloading everything, restarting socket connection. This is done to keep app live. because when user opens app from recents onStart gets triggered. Until this part everything is okay. Now I have image modal to view images and when user opens an image and comes back to main screen onStart is triggered and everything reset. So I need not to trigger onStart when user navigates to this page
5 Views