Scoping a viewmodel to multiple fragments (not activity) using the navigation component
S

4

32

I'm using the navigation component, I want a view model to be shared between a few fragments but they should be cleared when I leave the fragments (hence not scoping them to the activity) I'm trying to take the one activity many fragments approach. I have managed to achieve this using multiple nav hosts and scoping the fragments to it using getParentFragment but this just leads to more issues having to wrap fragments in other parent fragments, losing the back button working seamlessly and other hacks to get something to work that should be quite simple. Does anyone have a good idea on how to achieve this? I wondered if theres anything with getViewModelStore I could be using, given the image below I want to scope a view model to createCardFragment2 and use it in anything after it (addPredictions, editImageFragment, and others i haven't added yet), but then if I navigate back to mainFragment I want to clear the view models.

BTW I cant just call clear on mainFragment view model store as there are other view models here that shouldn't be cleared, I guess i want a way to tell the nav host what the parent fragment should be which I'm aware isn't going to be a thing, or a way to make the view model new if I'm navigating from mainFragment or cardPreviewFragment

nav graph

Shrill answered 8/6, 2019 at 10:18 Comment(2)
When you initialize your ViewModel using ViewModelProviders, you need to supply a context. In the case of a fragment, you put this, so the ViewModel life gets scoped to the fragment. ViewModelProviders is intelligent enough to distinguish between an activity or a fragment context. In case when registering observables, you should register observers in onActivityCreated using viewLifeCycleOwner as a context of observer. This makes the observer live according to a fragment`s lifecycle.Barfuss
Sorry but I don't think you understand the question, I'm well aware of how the view model gets its hooks to the lifecycleShrill
M
14

Yes, it's possible to scope a viewmodel to a navgraph now starting with androidx.navigation:*:2.1.0-alpha02. See the release notes here and an example of the API here. All you need to give is the R.id for your navgraph. I find it a bit annoying to use, though, because normally viewmodels are initialized in onCreate, which isn't possible with this scope because the nav controller isn't guaranteed to be set by your nav host fragment yet (I'm finding this is the case with configuration changes).

Also, if you don't want your mainFragment to be part of that scope, I would suggest taking it out and maybe using a nested nav graph.

Marley answered 21/6, 2019 at 0:17 Comment(6)
Could you post an example of how your using this? Currently I'm getting an error saying the nav graph isn't on the back stack which it definitely isShrill
Also I'm trying to get it through view model store owner which I think replaced view model storeShrill
Is it possible to use savedstatehandler inside sharedviewmodel ?Harkins
If we can't initialize it in onCreate, Where it should be initialized?Burgle
elaborated answer with a bit code here: https://mcmap.net/q/469869/-how-to-scope-a-view-model-to-a-parent-fragmentSiloum
Is there a way to know when start and stop happened in regards to this extended scope? For example if you hold some socketmanager and want it to be active in that nav graph only but also connect/disconnect when last fragment on stop happens?Nicaea
R
17

Here's a concrete example of Alex H's accepted answer.

In your build.gradle (app)

dependencies {
    def nav_version = "2.1.0"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
}

Example of view model

class MyViewModel : ViewModel() { 
    val name: MutableLiveData<String> = MutableLiveData()
}

In your FirstFlowFragment.kt define

val myViewModel: MyViewModel by navGraphViewModels(R.id.your_nested_nav_id)
myViewModel.name.value = "Cool Name"

And in your SecondFlowFragment.kt define

val myViewModel: MyViewModel by navGraphViewModels(R.id.your_nested_nav_id)
val name = myViewModel.name.value.orEmpty()
Log.d("tag", "welcome $name!")

Now the ViewModel is scoped in this nested fragment, shared state will be destroyed when nested nav is destroyed as well, no need to manually reset them.

Rustie answered 18/12, 2019 at 7:8 Comment(8)
I find weird that your ViewModel is implemented in (extends) the Fragment... of course the viewmodel will be scoped to the fragment: they are the same object.... I don't know if I'm missing something from your solution...Warram
Ohhh, sorry I wrote it really quick, fixed it.Rustie
That makes more sense.Warram
Hope you find my example useful!Rustie
Is it possible to use savedstatehandler inside sharedviewmodel ?Harkins
Here Noah an article for that: medium.com/@elye.project/…Rustie
Need to add defaultViewModelProviderFactory with Hilt. Eg: private val viewModel: SpaceListViewModel by navGraphViewModels(R.id.spaceFragment) { defaultViewModelProviderFactory }Idalia
I understand that whis will share the ViewModel across the fragments within the navGraph, but will it also be shared between Activities that are in my navGraph? ie I have some "DialogActivities" that are in my graph and I navigate to them using the navGraph directions so would the ViewModel be the same instance that the Fragments get?Robet
M
14

Yes, it's possible to scope a viewmodel to a navgraph now starting with androidx.navigation:*:2.1.0-alpha02. See the release notes here and an example of the API here. All you need to give is the R.id for your navgraph. I find it a bit annoying to use, though, because normally viewmodels are initialized in onCreate, which isn't possible with this scope because the nav controller isn't guaranteed to be set by your nav host fragment yet (I'm finding this is the case with configuration changes).

Also, if you don't want your mainFragment to be part of that scope, I would suggest taking it out and maybe using a nested nav graph.

Marley answered 21/6, 2019 at 0:17 Comment(6)
Could you post an example of how your using this? Currently I'm getting an error saying the nav graph isn't on the back stack which it definitely isShrill
Also I'm trying to get it through view model store owner which I think replaced view model storeShrill
Is it possible to use savedstatehandler inside sharedviewmodel ?Harkins
If we can't initialize it in onCreate, Where it should be initialized?Burgle
elaborated answer with a bit code here: https://mcmap.net/q/469869/-how-to-scope-a-view-model-to-a-parent-fragmentSiloum
Is there a way to know when start and stop happened in regards to this extended scope? For example if you hold some socketmanager and want it to be active in that nav graph only but also connect/disconnect when last fragment on stop happens?Nicaea
S
5

so when i posted this the functionality was there but didn't quite work as expected, since then i now use this all the time and this question keeps getting more attention so thought i would post an up to date example,

using

//Navigation
implementation "androidx.navigation:navigation-fragment:2.2.0-rc04"
// Navigation UI
implementation "androidx.navigation:navigation-ui:2.2.0-rc04"

i get the view model store owner like this

private ViewModelStoreOwner getStoreOwner() {

        NavController navController = Navigation
                .findNavController(requireActivity(), R.id.root_navigator_fragment);
        return navController.getViewModelStoreOwner(R.id.root_navigator);
}

im using the one activity multiple fragments implementation, but using this i can effectively tie my view models to just the scoped fragments and with the new live data you can even limit that too

the first id comes from the nav graphs fragment

<?xml version="1.0" encoding="utf-8"?>
  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <fragment
      android:id="@+id/root_navigator_fragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="androidx.navigation.fragment.NavHostFragment"
      app:defaultNavHost="true"
      app:navGraph="@navigation/root_navigator"/>

  </FrameLayout>

and the second comes from the id of the nav graph

  <?xml version="1.0" encoding="utf-8"?>
  <navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_navigator"
    app:startDestination="@id/mainNavFragment">

and then you can use it like so

private void setUpSearchViewModel() {
    searchViewModel = new ViewModelProvider(getStoreOwner()).get(SearchViewModel.class);
}
Shrill answered 5/3, 2020 at 22:14 Comment(1)
Do you use a separate fragment as NavHost? If yes, how do you add/remove the navhost fragment? Can you do it using Navigation as well?Grievance
T
1

So based on the answers here I made a function that lazily returns a ViewModel scoped to the current navigation graph.

     private val scopedViewModel by lazy { getNavScopedViewModel(arg) }


    /**
     * The [navGraphViewModels] function is not entirely lazy, as we need to pass the graph id
     * immediately, but we cannot call [findNavController] to get the graph id from, before the
     * Fragment's [onCreate] has been called. That's why we wrap the call in a function and call it lazily.
     */
    fun getNavScopedViewModel(arg: SomeArg): ScopedViewModel {
        // The id of the parent graph. If you're currently in a destination within this graph
        // it will always return the same id
        val parentGraphScopeId = findNavController().currentDestination?.parent?.id
            ?: throw IllegalStateException("Navigation controller should already be initialized.")
        val viewModel by navGraphViewModels<ScopedViewModel>(parentGraphScopeId) {
            ScopedViewModelFactory(args)
        }

        return viewModel
    }

It's not the prettiest implementation but it gets the job done

Transducer answered 8/12, 2021 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.