在使用刀柄进行依赖注射时,Android构图项目中的共享视图模型?

发布于 2025-01-30 08:08:28 字数 229 浏览 5 评论 0原文

任何人都有任何线索如何在不同的组合中保留共享的视图模型对象? 我正在使用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 技术交流群。

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

发布评论

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

评论(3

愚人国度 2025-02-06 08:08:28

您所需要的只是这样的东西是在导航后堆栈条目中找到视图模型,然后将其传递到下一个可复合的屏幕:

val backStackEntry = remember {
    navHostController.getBackStackEntry("first_screen_route_where_viewmodel_was_firstly_initialized")
}
val viewModel: MyViewModel = hiltViewModel(backStackEntry)

现在,您已经有了视图模型,该模型与以前的屏幕完全放在同一状态。
现在,您可以将其用作共享视图模型。
感谢@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:

val backStackEntry = remember {
    navHostController.getBackStackEntry("first_screen_route_where_viewmodel_was_firstly_initialized")
}
val viewModel: MyViewModel = hiltViewModel(backStackEntry)

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

如若梦似彩虹 2025-02-06 08:08:28

根据官方文档

如果您需要检索范围范围示为视图的实例
导航路由或导航图,请使用
HiltViewModel可组合功能并传递相应的功能
backstackentry作为参数:

import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.getBackStackEntry

@Composable
fun MyApp() {
    NavHost(navController, startDestination = startRoute) {
        navigation(startDestination = innerStartRoute, route = "Parent") {
            // ...
            composable("exampleWithRoute") { backStackEntry ->
                val parentEntry = remember(backStackEntry) {
                    navController.getBackStackEntry("Parent")
                }
                val parentViewModel = hiltViewModel<ParentViewModel>(parentEntry)
                ExampleWithRouteScreen(parentViewModel)
            }
        }
    }
}

As per the official documentation:

If you need to retrieve the instance of a ViewModel scoped to
navigation routes or the navigation graph instead, use the
hiltViewModel composable function and pass the corresponding
backStackEntry as a parameter:

import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.getBackStackEntry

@Composable
fun MyApp() {
    NavHost(navController, startDestination = startRoute) {
        navigation(startDestination = innerStartRoute, route = "Parent") {
            // ...
            composable("exampleWithRoute") { backStackEntry ->
                val parentEntry = remember(backStackEntry) {
                    navController.getBackStackEntry("Parent")
                }
                val parentViewModel = hiltViewModel<ParentViewModel>(parentEntry)
                ExampleWithRouteScreen(parentViewModel)
            }
        }
    }
}
贱贱哒 2025-02-06 08:08:28

根据 @gabhor的答案,您可以创建一个扩展功能,以避免到处编写相同的代码。

@Composable
inline fun <reified T : ViewModel> NavBackStackEntry.sharedViewModel(navController: NavController): T {
    val navGraphRoute = destination.parent?.route ?: return hiltViewModel()
    val parentEntry = remember(this) {
        navController.getBackStackEntry(navGraphRoute)
    }

    return hiltViewModel(parentEntry)
}

参考: https://github.dev/philipplackner/nestednavigationgraphsednavigationgraphsgraphsguide

According to @gabhor's answer you can create an extension function to avoid writing the same code everywhere.

@Composable
inline fun <reified T : ViewModel> NavBackStackEntry.sharedViewModel(navController: NavController): T {
    val navGraphRoute = destination.parent?.route ?: return hiltViewModel()
    val parentEntry = remember(this) {
        navController.getBackStackEntry(navGraphRoute)
    }

    return hiltViewModel(parentEntry)
}

Reference: https://github.dev/philipplackner/NestedNavigationGraphsGuide

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