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

Archie

09/28/2020, 1:48 PM
Is there a way to know whether a composable is disposed because it gets removed compared to when it is disposed because the activity holding it gets rotated?
y

Yann Badoual

09/28/2020, 2:13 PM
You could save the configuration state, and compare it on re-composition/disposition
a

Adam Powell

09/28/2020, 2:17 PM
I know this is going to be a tedious question, but what's your use case? We might already have a tool for what you're trying to do more specifically, or plans for one
a

Archie

09/29/2020, 1:50 PM
Hi @Adam Powell, I wanted to create a
ViewModelStore
which I could scope to a "composable screen" lifecycle. Kinda like how Fragment's ViewModel work but instead for "composable screen".
a

Adam Powell

09/29/2020, 2:07 PM
I think @Andrey Kulikov has been working in this area. For a kind of hacky way today you can probably take a look at the
ContextAmbient
to get the current context, unwrap its base context until you find an
Activity
instance, and check its
isChangingConfiguration
method
😮 1
a

Archie

09/29/2020, 2:11 PM
Sounds interesting... Ill have a look at it. Thanks!
a

Andrey Kulikov

09/29/2020, 4:56 PM
there is no simple way to implement it on your own I think. but we will have composable scoped ViewModelStore provided as a part of our navigation library compose integration which we work on atm
❤️ 2
a

Archie

09/30/2020, 7:38 AM
@Andrey Kulikov Wow! Thats some good news right there 😄 really looking forward to it.
hi @Andrey Kulikov, I've recently tried
Navigation Compose
Library which is currently in the SNAPSHOT. Is the feature you mentioned already in that version? If yes I'm curious and want to having a look at it.
a

Andrey Kulikov

10/10/2020, 3:25 PM
yes, as far as I remember it is there
a

Archie

10/10/2020, 3:55 PM
can you guide me which part I should take a look at? If this is a bit too much to ask, I apologize in advance.
here you can see that NavHost composable provides its own implementation as a store for viewmodels. now when you use viewModel composable inside the subtree of NavHost such view models will be bound to a lifecycle of this navigation destination
🔥 1
a

Archie

10/12/2020, 9:23 AM
I see... i was looking at the wrong thing... I though there would be a new API of sort just like activityViewModel() or something.. its awesome that it works with
viewModel()
thank you very much.
hi @Andrey Kulikov, is it possible to scope
ViewModel
across multiple
composable()
? Something like:
Copy code
NavHost(...) {
    composable("route1) {
        val viewModel: SharedViewModel = viewModel() // Where this ViewModel
        Screen(...)
    }
    composable("route2) {
        val viewModel: SharedViewModel = viewModel() // and this
        Screen(...)
    }
    composable("route3) {
        val viewModel: SharedViewModel = viewModel() // And this are all same instances
        Screen(...)
    }
    ....
}
please see this thread
5 Views