androidx.hilt.navigation.compose
Top-level functions summary
inline VM |
@Composable Returns an existing HiltViewModel -annotated |
inline VM |
@Composable Returns an existing HiltViewModel -annotated |
Top-level functions
hiltViewModel
@Composable
inline fun <VM : ViewModel> hiltViewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) { "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner" },
key: String? = null
): VM
Returns an existing HiltViewModel -annotated ViewModel
or creates a new one scoped to the current navigation graph present on the {@link NavController} back stack.
If no navigation graph is currently present then the current scope will be used, usually, a fragment or an activity.
import androidx.compose.runtime.remember import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.ViewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController val navController = rememberNavController() NavHost(navController, startDestination = "ExampleRoute") { composable("ExampleRoute") { val viewModel = hiltViewModel<ExampleViewModel>() } }
import androidx.compose.runtime.remember import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.ViewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController import androidx.navigation.navigation val navController = rememberNavController() NavHost(navController, startDestination = "Parent") { navigation(startDestination = "InnerRouteA", route = "Parent") { composable("InnerRouteA") { val parentEntry = remember { navController.getBackStackEntry("Parent") } val viewModel = hiltViewModel<ParentViewModel>(parentEntry) } composable("InnerRouteB") { val parentEntry = remember { navController.getBackStackEntry("Parent") } val viewModel = hiltViewModel<ParentViewModel>(parentEntry) } } }
hiltViewModel
@Composable
inline fun <VM : ViewModel, VMF : Any?> hiltViewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) { "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner" },
key: String? = null,
noinline creationCallback: (VMF) -> VM
): VM
Returns an existing HiltViewModel -annotated ViewModel
with an @AssistedInject-annotated constructor or creates a new one scoped to the current navigation graph present on the {@link NavController} back stack.
If no navigation graph is currently present then the current scope will be used, usually, a fragment or an activity.
import androidx.compose.runtime.remember import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.ViewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController val navController = rememberNavController() NavHost(navController, startDestination = "ExampleRoute") { composable("ExampleRoute") { val viewModel = hiltViewModel<ExampleViewModel>() } }
import androidx.compose.runtime.remember import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.ViewModel import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController import androidx.navigation.navigation val navController = rememberNavController() NavHost(navController, startDestination = "Parent") { navigation(startDestination = "InnerRouteA", route = "Parent") { composable("InnerRouteA") { val parentEntry = remember { navController.getBackStackEntry("Parent") } val viewModel = hiltViewModel<ParentViewModel>(parentEntry) } composable("InnerRouteB") { val parentEntry = remember { navController.getBackStackEntry("Parent") } val viewModel = hiltViewModel<ParentViewModel>(parentEntry) } } }