使用导航组件将视图模型范围限定为多个片段(不是活动) [英] Scoping a viewmodel to multiple fragments (not activity) using the navigation component

本文介绍了使用导航组件将视图模型范围限定为多个片段(不是活动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航组件,我希望在几个片段之间共享一个视图模型,但是当我离开这些片段时它们应该被清除(因此没有将它们限定到活动中)我正在尝试使用一个活动多片段方法.我设法使用多个导航主机实现了这一点,并使用 getParentFragment 将片段范围限定到它,但这只会导致更多问题必须将片段包装在其他父片段中,失去后退按钮无缝工作和其他黑客来获得一些应该工作的东西很简单.有没有人有关于如何实现这一目标的好主意?我想知道 getViewModelStore 是否有任何我可以使用的东西,鉴于下面的图像,我想将视图模型范围限定为 createCardFragment2 并在它之后的任何地方使用它(addPredictions、editImageFragment 和其他我尚未添加的),但是如果我导航回 mainFragment 我想清除视图模型.

I'm using the navigation component, I want a view model to be shared between a few fragments but they should be cleared when I leave the fragments (hence not scoping them to the activity) I'm trying to take the one activity many fragments approach. I have managed to achieve this using multiple nav hosts and scoping the fragments to it using getParentFragment but this just leads to more issues having to wrap fragments in other parent fragments, losing the back button working seamlessly and other hacks to get something to work that should be quite simple. Does anyone have a good idea on how to achieve this? I wondered if theres anything with getViewModelStore I could be using, given the image below I want to scope a view model to createCardFragment2 and use it in anything after it (addPredictions, editImageFragment, and others i haven't added yet), but then if I navigate back to mainFragment I want to clear the view models.

顺便说一句,我不能只在 mainFragment 视图模型存储上调用 clear,因为这里还有其他不应清除的视图模型,我想我想要一种方法来告诉导航主机我所知道的父片段应该是什么如果我从 mainFragment 或 cardPreviewFragment 导航,这不会是一件事,也不是一种使视图模型更新的方法

BTW I cant just call clear on mainFragment view model store as there are other view models here that shouldn't be cleared, I guess i want a way to tell the nav host what the parent fragment should be which I'm aware isn't going to be a thing, or a way to make the view model new if I'm navigating from mainFragment or cardPreviewFragment

推荐答案

是的,现在可以从 androidx.navigation:*:2.1.0-alpha02 开始将视图模型的范围限定为导航图.请参阅此处的发行说明和 API 示例此处.您需要提供的只是导航图的 R.id.不过,我觉得使用起来有点烦人,因为通常视图模型是在 onCreate 中初始化的,这在此范围内是不可能的,因为导航控制器不能保证由您的导航主机片段设置然而(我发现这是配置更改的情况).

Yes, it's possible to scope a viewmodel to a navgraph now starting with androidx.navigation:*:2.1.0-alpha02. See the release notes here and an example of the API here. All you need to give is the R.id for your navgraph. I find it a bit annoying to use, though, because normally viewmodels are initialized in onCreate, which isn't possible with this scope because the nav controller isn't guaranteed to be set by your nav host fragment yet (I'm finding this is the case with configuration changes).

此外,如果您不希望您的 mainFragment 成为该范围的一部分,我建议将其取出并使用 嵌套导航图.

Also, if you don't want your mainFragment to be part of that scope, I would suggest taking it out and maybe using a nested nav graph.

这篇关于使用导航组件将视图模型范围限定为多个片段(不是活动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆