在使用刀柄进行依赖注射时,Android构图项目中的共享视图模型?
任何人都有任何线索如何在不同的组合中保留共享的视图模型对象? 我正在使用HILT和使用HILT IN Composable注射ViewModel实例。 基本上,有3个屏幕共享相同的数据和我想共享的更改,我正在考虑通过共享视图模型共享此数据。
myViewModel: MyViewModel = hiltViewModel()
那么如何将此myViewModel用作共享视图模型?
Anyone has any clue how to retain a shared view model object across different composables?
I'm using hilt and injecting viewmodel instance using hilt in composable.
Basically there are 3 screens which share same data and changes I want to share it and I'm thinking of sharing this data through a shared view model.
myViewModel: MyViewModel = hiltViewModel()
So how can i use this MyViewModel as shared view model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所需要的只是这样的东西是在导航后堆栈条目中找到视图模型,然后将其传递到下一个可复合的屏幕:
现在,您已经有了视图模型,该模型与以前的屏幕完全放在同一状态。
现在,您可以将其用作共享视图模型。
感谢@pylyp的指导。
All you need is something like this to find the view model in your navigation back stack entry and pass it to next composable screen:
Now you have got the view model which is exactly at same state where you have left it in previous screens.
Now you can use it as a shared view model.
Thanks @Pylyp for guidance..
根据官方文档:
As per the official documentation:
根据 @gabhor的答案,您可以创建一个扩展功能,以避免到处编写相同的代码。
参考: https://github.dev/philipplackner/nestednavigationgraphsednavigationgraphsgraphsguide
According to @gabhor's answer you can create an extension function to avoid writing the same code everywhere.
Reference: https://github.dev/philipplackner/NestedNavigationGraphsGuide