Hi All, please help me to understand when view get...
# compose
p
Hi All, please help me to understand when view gets unloaded from the app in compose ? I want to fire an event when screen gets unloaded. Expectation is like fragments - FragA (screen started) -> move to -> FragB (screen started but FragA not destroyed as its live in background ) -> back to FragA (FragB should call unload event). (
s
By Frag you mean Composable?
p
My expectation is same behaviour as fragment. But not able to understand how compose works. I tested on JetNews goggle sample and found this - Land on HomeFeedScreen ***************** com.example.jetnews D ON_CREATE - HomeFeedScreen ***************** com.example.jetnews D ON_START - HomeFeedScreen ***************** com.example.jetnews D ON_RESUME - HomeFeedScreen Navigated to ArticleScreen ***************** com.example.jetnews D onDispose - HomeFeedScreen ***************** com.example.jetnews D ON_CREATE - ArticleScreen ***************** com.example.jetnews D ON_START - ArticleScreen ***************** com.example.jetnews D ON_RESUME - ArticleScreen Navigated back to HomeFeedScreen ***************** com.example.jetnews D onDispose - ArticleScreen ***************** com.example.jetnews D ON_CREATE - HomeFeedScreen ***************** com.example.jetnews D ON_START - HomeFeedScreen ***************** com.example.jetnews D ON_RESUME - HomeFeedScreen Clicked Home button of the app and app is in background ***************** com.example.jetnews D ON_PAUSE - HomeFeedScreen ***************** com.example.jetnews D ON_STOP - HomeFeedScreen Again open the app from the background ***************** com.example.jetnews D ON_START - HomeFeedScreen ***************** com.example.jetnews D ON_RESUME - HomeFeedScreen
s
Yes Composables are being destroyed once they are not in the hierarchy anymore
I’m not aware btw how the JetNews app works under the hood
p
But I can not see OnDestroy is getting called anywhere. So does it mean everytime composable screens gets unloaded from app when we move to next screen or come back to previous screen ?
s
But you see
onDispose
instead… (however I’m not sure where are these logs are coming from) The short answer is yes, if you don’t see it on the screen it is destroyed
p
Okay thank you so much for the answer @Sergey Dmitriev. If anyone have different opinion please pitch in.
🙌 1
One more question @Sergey Dmitriev - If I am seeing ON_CREATE invoked everytime on landing on the screen either landing for the first time or coming back on the same screen, does it mean every time view gets created ? So If I send start event for that screen on on_create then would it correct?
s
To answer this I need to know where are ON_CREATE, ON_START etc. are coming from, it looks like Fragment lifecycle to me. Composable lifecycle is a bit different, you can detect similar events though:
Copy code
@Composable
fun Screen() {
  LaunchedEffect(Unit) {
    // Screen created
  }

  DisposableEffect(Unit) {
    onDispose { 
      // Screen destroyed
    }
  }
}
p
Copy code
@Composable
fun Decorator(
    content: @Composable () -> Unit
) {
    val lifecycleOwner = LocalLifecycleOwner.current
    DisposableEffect(lifecycleOwner) {
        val observer = LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> {
                    // Send page start event
                }
                else -> {
                }
            }
        }

        lifecycleOwner.lifecycle.addObserver(observer)
        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
            // Send page unload event
        }
    }
    content()
}
This is the code
s
Okay, and this tracks the lifecycle owner’s lifecycle, not a given composable 🙂
What are you trying to do?
p
I want to track when screen started and screen unloaded from the app - • Start when a page has started loading. • Unload: the user exits the page and the app unload the related view
s
What is a screen/page in your case? Is it an Activity or Fragment or Composable?
p
Composable
s
Then you can use:
Copy code
DisposableEffect(Unit) {
  // Screen created and displayed
  onDispose {
    // Screen removed and destroyed
  }
}
lifecycleOwner
may be pointing to different things: Activity, Fragment or NavHost depending on your setup, so it has nothing to do with your Composable
p
Thanks
🙌 1