injecting viewmodel with navigation-graph scope: NavController is not available before onCreate()

前端 未结 2 1688
青春惊慌失措
青春惊慌失措 2021-01-05 10:49

I\'m using a navigation-component in my application and also using shared ViewModel between multiple fragments that are in the same graph. Now I want to instantiate the View

2条回答
  •  离开以前
    2021-01-05 11:24

    ...we should inject objects ( ViewModel,..etc ) in onAttach...

    Looks like it is currently a no go for such injection with the original by navGraphViewModels(R.id.nav_graph) delegated property provided by androidx.navigation package because from the source code

    findNavController().getBackStackEntry(navGraphId) and

    public final NavController getNavController() it stated that:

     * Returns the {@link NavController navigation controller} for this navigation host.
     * This method will return null until this host fragment's {@link #onCreate(Bundle)}
    

    And here are some workarounds:

    https://github.com/InsertKoinIO/koin/issues/442

提交回复
热议问题