JAVA中fragment和activity之间共享工厂视图模型

发布于 2025-01-12 22:29:04 字数 1594 浏览 0 评论 0原文

我有这个

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2025-01-19 22:29:04

当您在 Fragment 中使用 by ActivityViewModels() 时,相当于在 Activity 中使用 by viewModels()。无论哪种方式,它都是一个以关联的 Activity 生命周期为范围的 ViewModel。

由于您希望在 Activity 中引用 ViewModel 并且希望将其保留为 Java,因此您不能使用属性委托。这相当于 Activity 中的 by viewModels { ForceUpdateViewModelFactory() }

private ForceUpdateViewModelFactory viewModel;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //...

    viewModel = new ViewModelProvider(this, new ForceUpdateViewModelFactory())
        .get(ForceUpdateViewModel.class);

    //...
}

When you use by activityViewModels() in a Fragment, that's equivalent to using by 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:

private ForceUpdateViewModelFactory viewModel;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //...

    viewModel = new ViewModelProvider(this, new ForceUpdateViewModelFactory())
        .get(ForceUpdateViewModel.class);

    //...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文