简体   繁体   English

通过 navGraphViewModel 导航返回时保留 ViewModel 实例

[英]Keep ViewModel instance when navigating back with by navGraphViewModel

So I am following Google's approach where you use single activity for the whole app and multiple view models, which are scoped to your nested nav graphs or fragments.因此,我遵循 Google 的方法,您对整个应用程序和多个视图模型使用单个活动,这些模型的范围仅限于您的嵌套导航图或片段。

And I have a problem with this approach.我对这种方法有疑问。 Let's use example provided by google docs so it would be easier for me.让我们使用谷歌文档提供的示例,这样对我来说会更容易。

循环逻辑

Here when user reached destination C, the back stack contains one instance of each destination(A, B, C) and if we want to return to destination A without multiplying instances of our destinations inside back stack - we need to add popUpTo="@+id/a" and popUpToInclusive="true" to clear all instances inside the back stack.在这里,当用户到达目的地 C 时,后退堆栈包含每个目的地(A、B、C)的一个实例,如果我们想返回目的地 A 而不在后退堆栈中增加目的地的实例 - 我们需要添加popUpTo="@+id/a"popUpToInclusive="true"清除后栈中的所有实例。

Here is the problem - if we will use this then every old instance of destinations will be cleared and new instance of A will be created hence if we are using by navGraphViewModel(R.id.A) then our old instance of ViewModel will be cleared as well!这就是问题所在 - 如果我们将使用它,那么每个旧的目的地实例都将被清除,并且将创建新的 A 实例因此如果我们使用by navGraphViewModel(R.id.A)那么我们的旧 ViewModel 实例将被清除也!

So how to avoid such behavior?那么如何避免这种行为呢? Is there a way to open destination A without creating a new instance so that our view model won't be cleared as well?有没有办法在不创建新实例的情况下打开目标 A,这样我们的视图 model 也不会被清除?

I have tried multiple ways to prevent that, but nothing seemed to work (in this case A - could be a nested graph also and that would work the same way)我尝试了多种方法来防止这种情况,但似乎没有任何效果(在这种情况下 A - 也可能是嵌套图,并且工作方式相同)

Edit: I Just read your question again and I think I misunderstood it the first time.编辑:我刚刚再次阅读了您的问题,我想我第一次误解了它。 My answer might not be of much help.我的回答可能帮不上什么忙。

Is your requirement to have sub (nested) NavGraphs?您是否需要子(嵌套)NavGraphs? If not, I don't think navGraphViewModels() applies to you.如果不是,我认为navGraphViewModels()用于您。 From what I read, viewmodels created using this approach are cleared when their corresponding NavGraph is completed.根据我的阅读,使用这种方法创建的视图模型在完成相应的 NavGraph 后会被清除。

navGraphViewModels is very useful when we are creating fragment for some modules and we don't want these viewModels to be persist throughout the whole activity.当我们为某些模块创建片段并且我们不希望这些视图模型在整个活动中持续存在时,navGraphViewModels 非常有用。 Hence, we can create our own custom sub-navGraph with navGraphViewModels().因此,我们可以使用 navGraphViewModels() 创建我们自己的自定义子导航图。

That is, if you simply want to achieve a single activity / multiple fragments architecture, why not use by activityViewModels() inside each fragment.也就是说,如果你只是想实现单个活动/多个片段架构,为什么不使用每个片段内部by activityViewModels() Using this, the viewmodel instance is persisted as long as the host activity is alive.使用它,只要主机活动还活着,视图模型实例就会被持久化。 Thus allowing you to share data between the activity and all the fragments.从而允许您在活动和所有片段之间共享数据。

Reference: https://skynight1996.medium.com/navigation-component-comparison-between-viewmodels-activityviewmodels-and-ae0145734228参考: https://skynight1996.medium.com/navigation-component-comparison-between-viewmodels-activityviewmodels-and-ae0145734228

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回时刷新viewmodel中的数据,android - refresh data in viewmodel when navigating back, android 建议在应用程序中导航时保持数据持久 - Advises to keep data persisted when navigating back in application 在 Android 中导航时如何保留 ViewModel? - How to persist ViewModel when navigating in Android? 片段中的ViewModel + LiveData,从ItemDetailFragment导航回去时如何保留ListItemFragment的数据 - ViewModel + LiveData in Fragment, How to keep data of the ListItemFragment when navigate back from ItemDetailFragment 当我按回来时导航片段的问题 - Problem with navigating fragments when i press on back 导航回时避免 API 调用可组合项 - Avoid API call in composable when navigating back to it 导航返回时的 RecyclerView(导航组件) - RecyclerView when navigating back (Navigation Component) 导航回MvxTabsFragmentActivity时,应用程序崩溃 - App crashes when navigating back to MvxTabsFragmentActivity 导航回来时,Android工具栏变得半透明 - Android Toolbar becomes translucent when navigating back 使用 ViewModel 更改片段时,将 Bitmap 保留在 ImageView 中 - Keep Bitmap in ImageView when Change Fragment with ViewModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM