带有共享视图模型的Android导航组件 [英] Android navigation component with shared view models

查看:66
本文介绍了带有共享视图模型的Android导航组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个视图模型与一个活动或它所附着的片段一起生存和死亡.这有一定的影响,这超出了我的范围,为什么没有人问(如果我们将Navigation体系结构放入图片中).

根据最新的android博客和导航框架的工作方式,建议使用单活动多片段"一节.

据说我有以下应用程序设计.

Activity A (Application Entry Point)
----------
Fragment (A) (Uses ViewModel AB)
Fragment (B) (Uses ViewModel AB)
Fragment (C) (Uses ViewModel CDE)
Fragment (D) (Uses ViewModel CDE)
Fragment (E) (Uses ViewModel CDE)

现在,由于我使用共享的视图模型,这意味着我的视图模型将附加到活动中.但是,这似乎是泄漏的.就像我从A到E一直走到现在又从片段弹出到片段B一样,视图模型CDE应该被销毁,但是不会被破坏,因为它已与活动连接.

我们也无法将视图模型连接到片段,因为我们将共享它们的数据.

只有我提出这个问题的事实使我相信,根据我的理解,我在这里是错误的.如果我能对情况有个适当的了解,那会很高兴.

解决方案

这确实是一个问题,已经报告给Google .

幸运的是,由于Navigation 2.1.0-alpha02(在2.1.0中稳定),此问题已得到解决.您可以在此处文档.

现在,您可以通过Kotlin用户的by navGraphViewModels()属性委托或使用添加到NavControllergetViewModelStore() API来创建在导航图级别范围内的ViewModel.

首先,您应该在导航图设计器中选择一些片段,然后右键单击它们,然后选择Move to Nested Graph创建一个新的图,将其用作范围",如下所示:

class DetailFr : Fragment() {
    private val vm: DetailViewModel by navGraphViewModels(R.id.main_nav_graph)
}

您可以在Nested Graph 此处了解更多信息.. >

A viewmodel lives and dies with an activity or the fragment it is attached to. This has certain ramifications that it is beyond me why isn't anyone asking about (if we get the Navigation architecture into the picture).

According to the latest android blogs and the way navigation framework works , we are recommended to go in the Single Activity Multiple Fragments verse.

Supposedly I have the following app design .

Activity A (Application Entry Point)
----------
Fragment (A) (Uses ViewModel AB)
Fragment (B) (Uses ViewModel AB)
Fragment (C) (Uses ViewModel CDE)
Fragment (D) (Uses ViewModel CDE)
Fragment (E) (Uses ViewModel CDE)

Now since I use shared viewmodels that means my viewmodels would be attached to the activity. However this appears to be leaky. Like if I have traversed all the way from A to E and now come back popping off fragments to fragment B , the viewmodel CDE should be destroyed , but it wont be since it is connected to the activity.

Also we cannot connect our viewmodels to the fragment since we are going to be sharing their data.

The fact that only I am raising this question makes me believe i am at mistake here with my understanding. Would be elated if I could be given a proper insight into the situation.

解决方案

This is really a problem and has been reported to Google.

Fortunately since Navigation 2.1.0-alpha02 (stable in 2.1.0) this problem has been solved. You can find the change log here and the document.

You can now create ViewModels that are scoped at a navigation graph level via the by navGraphViewModels() property delegate for Kotlin users or by using the getViewModelStore() API added to NavController.

First you should select some of fragments in your nav graph designer, then right click on them and choice Move to Nested Graph to create a new graph which will be used as a 'scope' like this:

class DetailFr : Fragment() {
    private val vm: DetailViewModel by navGraphViewModels(R.id.main_nav_graph)
}

You can learn more about Nested Graph here.

这篇关于带有共享视图模型的Android导航组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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