tivity中使用参数定义的Fragment中的ViewMode

tivity中使用参数定义的Fragment中的ViewMode

本文介绍了获取在Activity中使用参数定义的Fragment中的ViewModel的相同实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用Koin进行依赖项注入,这是我在活动中所做的事情

So, I am using Koin for dependency injection, Here is what I did inside a activity

class ModuleDetailActivity : AppCompatActivity() {

    private lateinit var moduleId:String
    private lateinit var levelModule:Level.Module

    private val moduleViewModel: ModuleViewModel by viewModel { parameterOf(moduleId, levelModule) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        ...
        ...

        moduleId = intent.getString("module_id")
        levelModule = intent.getParcelable("level_module")

        ...
        ...
    }
}

现在,我有多个ModuleDetailActivity可以添加或替换的片段,并且我希望这些片段中的moduleViewModel具有相同的实例,而无需在Fragment中传递任何参数.

Now, I have multiple fragment which ModuleDetailActivity can add or replace and I want the same instance of moduleViewModel in those fragments without passing any parameters inside Fragment.

class ModuleDetailFragment : Fragment() {

    private val moduleViewModel: ModuleViewModel by sharedViewModel()

    ...
    ...
}

我知道这会引发错误,并且按预期,您可以看到此

I know this will throw a error and as expected you can see this

Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [Factory:'****.ui.module.ModuleViewModel']

这就是我初始化模块的方式

This is how I have initialized the module

val viewModelModule = module {
    viewModel { (id : String, levelModule:Level.Module) -> ModuleViewModel(id, levelModule, get()) }
}

在不传递参数到Fragment内部的情况下,如何获得活动中定义的ModuleViewModel的相同实例的解决方案吗?

Is there any solution on how I can get the same instance of ModuleViewModel defined inside activity without passing parameter inside a Fragment?

推荐答案

使用 KOIN

ViewModel实例可以在Fragments及其主机Activity之间共享.

ViewModel instance can be shared between Fragments and their host Activity.

要在Fragment中注入共享的ViewModel,请使用:

To inject a shared ViewModel in a Fragment use:

by sharedViewModel() - lazy delegate property to inject shared ViewModel instance into a property
getSharedViewModel() - directly get the shared ViewModel instance

只需声明一次ViewModel:

Just declare the ViewModel only once:

val weatherAppModule = module {

   // WeatherViewModel declaration for Weather View components
   viewModel { WeatherViewModel(get(), get()) }

}

在您的活动中:

class WeatherActivity : AppCompatActivity() {

    /*
     * Declare WeatherViewModel with Koin and allow constructor dependency injection
     */
    private val weatherViewModel by viewModel<WeatherViewModel>()
}

在您的片段中:

class WeatherHeaderFragment : Fragment() {

    /*
     * Declare shared WeatherViewModel with WeatherActivity
     */
    private val weatherViewModel by sharedViewModel<WeatherViewModel>()
}

其他片段:

class WeatherListFragment : Fragment() {

    /*
     * Declare shared WeatherViewModel with WeatherActivity
     */
    private val weatherViewModel by sharedViewModel<WeatherViewModel>()
}

这篇关于获取在Activity中使用参数定义的Fragment中的ViewModel的相同实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:05