在Android组合导航中,导航参数以 savedstatehandle
填充,然后可以在 viewModel
中访问。我在实践中做到了这一点,并且在此处概述了实施: 。
我的问题是我找不到有关为什么/如何发生的任何文件。实际上,我什至找不到官方文档,甚至没有提及 SavedStateHandle
中的导航参数。我很想阅读更多有关此的信息。我已经阅读了这些文档(以及更多),但没有在主题上找到:
https://develiper.android.com/refer.com/redroid.com/reference./android.com/referenc./redroid/reference/referferenc./androidx/androidx/lifecycle/lifecycle/savedstate,state-.---/-/-/-/
这似乎是通过导航论证传递的一种非常好的技术,但是由于我在任何地方都没有考虑过渡到另一种技术。
In Android Compose navigation, navigation arguments are populated in SavedStateHandle
which can then be accessed in a ViewModel
. I have done this in practice and implementation is well outlined here: https://stackoverflow.com/a/69145748/12385518.
My problem is I cannot find any documentation on why/how this is happening. In fact I cannot even find official documentation that even mention navigation arguments being in SavedStateHandle
at all. I would love to read more about this. I have read these documents (and more) but found nothing on the topic:
https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate
https://developer.android.com/reference/androidx/lifecycle/SavedStateHandled
It seems to be a very nice technique for passing around navigation arguments, however since it isn't mentioned anywhere I am considering transitioning to another technique.
发布评论
评论(1)
这是将导航参数传递给
viewModel
的方式。呼叫
navcontroller.navigate(route,args)
anavbackstackentry
是创建的()。navbackstackentry
创建顾名思义,它是使用ViewModel
ssavedstatehandle
s创建navbackStackentry
实现hasdefaultViewModelProviderFactory
,并通过default> defaultviewModelProviderFactory
property从上一步公开其工厂.android.com/androidx/platform/frameworks/support/+/androidx-main:navigation/navigation-common/src/main/java/androidx/navigation/NavBackStackEntry.kt;l=218?q=NavBackStackEntry&ss=androidx “ rel =“ noreferrer”>源)。至关重要的是,
HASDEFAULTVIEWMODELPROVIDERFACTORY
还具有另一个属性:defaultViewModeLcreationExtras
。这包含构建ViewModel
时使用的额外数据字段。导航参数存储在此处( source )。当
viewModel
创建方法(例如hiltviewModel
)被调用,它将检查是否view> viewmodelstoreowner
(在这种情况下,> navbackstackentry
)实现HASDEFAULTVIEWMODELPROVIDERFACTORY
。如果是这样,那么它将将那些额外的(导航参数)传递到视图模型工厂(视图模型工厂,在这种情况下
savedStateViewModeFactory
,然后创建savedStateHandle
,将其填充使用导航参数(Extras),并将其提供给新创建的新创建viewModel
因此它具有访问权限(Here's how navigation arguments are passed to a
ViewModel
.When you call
NavController.navigate(route, args)
aNavBackStackEntry
is created (source).NavBackStackEntry
creates aSavedStateViewModelFactory
which, as its name suggests, is a factory for creatingViewModel
s with aSavedStateHandle
(source).NavBackStackEntry
implementsHasDefaultViewModelProviderFactory
and exposes its factory from the previous step via thedefaultViewModelProviderFactory
property (source).Crucially,
HasDefaultViewModelProviderFactory
also has another property:defaultViewModelCreationExtras
. This contains extra data fields which are used when constructing aViewModel
. The navigation arguments are stored here (source).When a
viewModel
creation method, such ashiltViewModel
, is called it will check whether theViewModelStoreOwner
(in this case theNavBackStackEntry
) implementsHasDefaultViewModelProviderFactory
. If it does, then it'll pass those extras (the navigation arguments) to the view model factory (source).The view model factory, in this case
SavedStateViewModelFactory
, then creates theSavedStateHandle
, populates it with the navigation arguments (the extras), and supplies it to a newly createdViewModel
so it has access to them (source).