SavedStateHandle如何提供导航参数?

发布于 2025-01-28 19:01:33 字数 882 浏览 3 评论 0 原文

在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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

客…行舟 2025-02-04 19:01:33

这是将导航参数传递给 viewModel 的方式。

  1. 呼叫

    navcontroller.navigate(route,args) a navbackstackentry 是创建的()。


  2. navbackstackentry 创建顾名思义,它是使用 ViewModel s savedstatehandle s创建 )。

  3. 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”>源)。

  4. 至关重要的是, HASDEFAULTVIEWMODELPROVIDERFACTORY 还具有另一个属性: defaultViewModeLcreationExtras 。这包含构建 ViewModel 时使用的额外数据字段。导航参数存储在此处( source )。

  5. viewModel 创建方法(例如 hiltviewModel )被调用,它将检查是否 view> viewmodelstoreowner (在这种情况下, > navbackstackentry )实现 HASDEFAULTVIEWMODELPROVIDERFACTORY 。如果是这样,那么它将将那些额外的(导航参数)传递到视图模型工厂(

  6. 视图模型工厂,在这种情况下 savedStateViewModeFactory ,然后创建 savedStateHandle ,将其填充使用导航参数(Extras),并将其提供给新创建的新创建 viewModel 因此它具有访问权限(

Here's how navigation arguments are passed to a ViewModel.

  1. When you call NavController.navigate(route, args) a NavBackStackEntry is created (source).

  2. NavBackStackEntry creates a SavedStateViewModelFactory which, as its name suggests, is a factory for creating ViewModels with a SavedStateHandle (source).

  3. NavBackStackEntry implements HasDefaultViewModelProviderFactory and exposes its factory from the previous step via the defaultViewModelProviderFactory property (source).

  4. Crucially, HasDefaultViewModelProviderFactory also has another property: defaultViewModelCreationExtras. This contains extra data fields which are used when constructing a ViewModel. The navigation arguments are stored here (source).

  5. When a viewModel creation method, such as hiltViewModel, is called it will check whether the ViewModelStoreOwner (in this case the NavBackStackEntry) implements HasDefaultViewModelProviderFactory. If it does, then it'll pass those extras (the navigation arguments) to the view model factory (source).

  6. The view model factory, in this case SavedStateViewModelFactory, then creates the SavedStateHandle, populates it with the navigation arguments (the extras), and supplies it to a newly created ViewModel so it has access to them (source).

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