JAVA中fragment和activity之间共享工厂视图模型
我有这个
class ForceUpdateViewModelFactory(
private val mApplication: Application,
private val mForceType: ForceUpdateType
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
require(modelClass == ForceUpdateViewModel::class.java) {
"Unsupported model class: $modelClass"
}
return ForceUpdateViewModel(mApplication, mForceType) as T
}
}
,我在 Fragment
中使用它。
class ForceUpdateViewModel(
application: Application,
forceType: ForceUpdateType
) :
AndroidViewModel(application) {
private val mUpdatePickataleEvent = LiveEvent<Void>()
private val mMaybeLaterEvent = LiveEvent<Void>()
val maybeLaterBtnVisibility: LiveData<Int> = getApplication<App>().configManager
.effectiveConfigurationSingle
.map { config ->
if (forceType == ForceUpdateType.HARD) View.VISIBLE else View.GONE
}.toObservable().asLiveData()
fun getUpdatePickataleEvent(): LiveData<Void> = mUpdatePickataleEvent
fun getMaybeLaterEvent(): LiveData<Void> = mMaybeLaterEvent
fun onUpdatePickataleBtnClick() {
mUpdatePickataleEvent.raise()
}
fun onMaybeLaterBtnClick() {
mMaybeLaterEvent.raise()
}
}
问题是我想监听父 Activity 中的 getMaybeLaterEvent,但该 Activity 使用 Java 语言。在这种情况下,我不知道如何与活动共享视图模型。 通常,我认为,我会使用 val shareViewModel: MyViewModel by ActivityViewModels()
或类似的东西来做到这一点,但我不知道如何在爪哇。
I have this
class ForceUpdateViewModelFactory(
private val mApplication: Application,
private val mForceType: ForceUpdateType
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
require(modelClass == ForceUpdateViewModel::class.java) {
"Unsupported model class: $modelClass"
}
return ForceUpdateViewModel(mApplication, mForceType) as T
}
}
and I use it in a Fragment
.
class ForceUpdateViewModel(
application: Application,
forceType: ForceUpdateType
) :
AndroidViewModel(application) {
private val mUpdatePickataleEvent = LiveEvent<Void>()
private val mMaybeLaterEvent = LiveEvent<Void>()
val maybeLaterBtnVisibility: LiveData<Int> = getApplication<App>().configManager
.effectiveConfigurationSingle
.map { config ->
if (forceType == ForceUpdateType.HARD) View.VISIBLE else View.GONE
}.toObservable().asLiveData()
fun getUpdatePickataleEvent(): LiveData<Void> = mUpdatePickataleEvent
fun getMaybeLaterEvent(): LiveData<Void> = mMaybeLaterEvent
fun onUpdatePickataleBtnClick() {
mUpdatePickataleEvent.raise()
}
fun onMaybeLaterBtnClick() {
mMaybeLaterEvent.raise()
}
}
The problem is that I would like to listen for the getMaybeLaterEvent
in the parent Activity, but that Activity is in Java. I have no idea how to share the view model with the Activity in this case.
Normally, I THINK, I'd do it by using val sharedViewModel: MyViewModel by activityViewModels()
or something of the sorts, but i have no idea how to do that in Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 Fragment 中使用
by ActivityViewModels()
时,相当于在 Activity 中使用by viewModels()
。无论哪种方式,它都是一个以关联的 Activity 生命周期为范围的 ViewModel。由于您希望在 Activity 中引用 ViewModel 并且希望将其保留为 Java,因此您不能使用属性委托。这相当于 Activity 中的
by viewModels { ForceUpdateViewModelFactory() }
:When you use
by activityViewModels()
in a Fragment, that's equivalent to usingby viewModels()
in an Activity. It's a ViewModel scoped to the associated Activity lifecycle either way.Since you want a ViewModel reference in your Activity and you want to keep it Java, you can't use a property delegate. This would be the equivalent of
by viewModels { ForceUpdateViewModelFactory() }
in the Activity: