Koin: SharedViewModel from parent fragment

Created on 18 Mar 2020  Â·  2Comments  Â·  Source: InsertKoinIO/koin

Hi, I was using sharedViewModel extension and now I see that the parameter "from" was deleted on latest version. Now the sharedviewmodel only works with hosting activity. Is not possible now to use this feature with another owner than the activity?

question

Most helpful comment

Well, you can use requireParentFragment() just like this:

private val filterControlViewModel: FilterControlViewModel by lazy {
        requireParentFragment().getViewModel<FilterControlViewModel>()
    }

All 2 comments

+1, i can't get viewmodel from parentFragment
i write a fun by myself

inline fun <reified T : ViewModel> Fragment.sharedViewModel(
        qualifier: Qualifier? = null,
        noinline from: ()->ViewModelStoreOwner = { activity as ViewModelStoreOwner },
        noinline parameters: ParametersDefinition? = null
): Lazy<T> = lazy{
     getKoin().getViewModel<T>(
            from(),
            qualifier,
            parameters
    )
}

Well, you can use requireParentFragment() just like this:

private val filterControlViewModel: FilterControlViewModel by lazy {
        requireParentFragment().getViewModel<FilterControlViewModel>()
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

iRYO400 picture iRYO400  Â·  3Comments

guymclean picture guymclean  Â·  3Comments

TedHoryczun picture TedHoryczun  Â·  4Comments

luna-vulpo picture luna-vulpo  Â·  4Comments

erikhuizinga picture erikhuizinga  Â·  3Comments