Hi folks, In compose multiplatform, suppose on bac...
# compose
s
Hi folks, In compose multiplatform, suppose on back press we need to clear some state data from screen viewModel so that next time screen works properly, how to do that, becoz in android we can use BackHandler for that?, But how to do that kind of stuff in compose multiplatform that works for all platforms?
a
s
Thanku guys. 🙌
👍 1
s
we need to clear some state data from screen viewModel so that next time screen works properly
You shouldn't have to do anything in a BackHandler for this. You should be getting a brand new ViewModel after you've popped a destination completely and you go back into it. What are you using for navigation? And how are you grabbing your viewmodel there? Is it scoped properly only to that destination's lifecycle?
s
Exactly, i was also expecting this, but not sure why it didnt happened. Im passing viewmodel in screen route composable and using compose navigation
s
How are you grabbing the viewmodel then? Where are you calling the function which creates the viewmodel?
s
Directly injecting into screen route method as parameter using koinInject
s
It'd be best to paste the code here inside your
composable<Screen> { ... }
function
s
Ok, i will check with this way. I was doing it like this,
Copy code
@Composable
fun SearchRoute(
    modifier: Modifier = Modifier,
    searchViewModel: SearchViewModel = koinInject(),
) { ... }
Now u mean, i should do it like this?
Copy code
composable<Screen> { 
   val searchViewModel: SearchViewModel = koinInject()

   searchScreenNavGraph(searchViewModel)
}
Let me try.
s
No honestly that should be the same thing, just one thing to confirm. If inside your screen you do
Copy code
@Composable
fun SearchRoute(
    modifier: Modifier = Modifier,
    searchViewModel: SearchViewModel = koinInject(),
) { 
  LaunchedEffect(searchViewModel) {
    Log.d("searchViewModel", "searchViewModel hashcode:${searchViewModel.hashCode()}")
  }
  ...
}
And you go to that screen, let it print something. Then go back, pop the backstack so this screen leaves the composition completely. And then you navigate to this screen again. Is the hashCode printed the same one as before?
s
Yea, thats what i was thinking they should be the same thing. And i tried hashCode, its coming as same, its weird.
s
AH! Do not use
koinInject
use
koinViewModel
😄 I completely missed that Replace
Copy code
searchViewModel: SearchViewModel = koinInject(),
with
Copy code
searchViewModel: SearchViewModel = koinViewModel(),
In your module definition, if you are doing:
Copy code
module {
 single<SearchViewModel> { SearchViewModel(get()) }
}
Replace with
Copy code
module {
 viewModel<SearchViewModel> { SearchViewModel(get()) }
}
s
Ok, is koinViewModel support available for multiplatform now? In which koin version?
I was not able to use it, thats why. Not sure which koin version supports koinViewModel for multiplatform, i will check that
Ah, i think i missed koin-compose-viewmodel, i thought koin-compose, include that too
s
Thnx, I was just thinking that koin-compose include viewModel api as well and multiplatform support is not available yet 😀. But just found out there is the separate extension for that.
a
Don’t you need to also scope the view model to the backstack entry in. Compose navigation
at least in hiltViewModel, you need to do
hiltViewModel(backstackEntry)
to make unique viewmodel per screen instance
i
The scoping is done automatically for you inside a
composable
destination since NavHost already sets the
LocalViewModelStoreOwner
to that destination (and that's the default value for
viewModel
, etc.)
thank you color 1
💯 1
161 Views