问题
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")
...
...
}
}
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()) }
}
Is there any solution on how I can get the same instance of ModuleViewModel
defined inside activity without passing parameter inside a Fragment
?
回答1:
Using KOIN
ViewModel instance can be shared between Fragments and their host Activity.
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
Just declare the ViewModel only once:
val weatherAppModule = module {
// WeatherViewModel declaration for Weather View components
viewModel { WeatherViewModel(get(), get()) }
}
In your activity:
class WeatherActivity : AppCompatActivity() {
/*
* Declare WeatherViewModel with Koin and allow constructor dependency injection
*/
private val weatherViewModel by viewModel<WeatherViewModel>()
}
In your fragment:
class WeatherHeaderFragment : Fragment() {
/*
* Declare shared WeatherViewModel with WeatherActivity
*/
private val weatherViewModel by sharedViewModel<WeatherViewModel>()
}
Other fragment:
class WeatherListFragment : Fragment() {
/*
* Declare shared WeatherViewModel with WeatherActivity
*/
private val weatherViewModel by sharedViewModel<WeatherViewModel>()
}
回答2:
You should initialise viewModel
as below:
class ModuleDetailActivity : AppCompatActivity() {
private val moduleId: String by lazy {
intent.getString("module_id")
}
private val levelModule: Level.Module by lazy {
intent.getParcelable("level_module")
}
private val moduleViewModel: ModuleViewModel by viewModel {
parameterOf(moduleId, levelModule)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
...
viewModel.doYourStuff()
...
...
}
}
We should use get()
or inject()
for plain Java objects only. To inject ViewModel
, they provided separate way as viewModel()
Not sure why it isn't working for you.
来源:https://stackoverflow.com/questions/60223209/get-the-same-instance-of-viewmodel-in-fragment-which-is-defined-in-activity-with