https://kotlinlang.org logo
#android
Title
# android
e

Eder Sanchez

06/15/2021, 6:40 AM
Copy code
Why enter twice?
``````
g

gildor

06/15/2021, 6:44 AM
Becauses of recomposition
you really shouldn’t do this
composable can be called multiple times, you do not handle lifecycle callback corretly, yu adding it on every recompoise
e

Eder Sanchez

06/15/2021, 6:48 AM
Copy code
is there another optimal way to know the life cycle? I need to know when it goes to destroy to free up resources
g

gildor

06/15/2021, 6:54 AM
What are you trying to achieve?
p

pepos

06/15/2021, 7:12 AM
your scope lifecycle should be driven by navigation and not by composition
I would add a OnDestinationChangedListener to the navController and control the lifecycle from there
e

Eder Sanchez

06/15/2021, 7:18 AM
Copy code
I need to delete the ExoPlayer instance
Copy code
in a fragment it would be canceling the onDestroy method, in this new approach I don't know very well where to release it, so I was looking for a way to know the life cycle
g

gildor

06/15/2021, 7:28 AM
So you want to release player onDestroy or when composable is removed from screen? It’s not the same
If you just want have fragment lifecycle, just create player onCreate and release onDestroy of fragment
if you want to scope it to composable, you probably need DisposableEffect: https://developer.android.com/jetpack/compose/side-effects#disposableeffect
e

Eder Sanchez

06/15/2021, 10:24 PM
Copy code
I found this way of doing it and I would like to know your opinion, as I understand it is only invoked once factory of AndroidView, it is there that when the screen is destroyed I call destroy () that is executed in the context of the activity and I can free the resources and destroy the ExoPlayer instance
g

gildor

06/17/2021, 6:17 AM
It still looks a bit strange for me, and just a pure hack
2 Views