我有一个名为 的可组合项ParentScreen和一个ViewModel名为ParentViewModel. 在 中ParentViewModel,我正在从我的存储库中收集一个值。
class MyRepo @Inject constructor() {
fun getParentData() = System.currentTimeMillis().toString() // some dummy value
}
@HiltViewModel
class ParentViewModel @Inject constructor(
myRepo: MyRepo
) : ViewModel() {
private val _parentData = MutableStateFlow("")
val parentData = _parentData.asStateFlow()
init {
val realData = myRepo.getParentData()
_parentData.value = realData
}
}
@Composable
fun ParentScreen(
parentViewModel: ParentViewModel = hiltViewModel()
) {
val parentData by parentViewModel.parentData.collectAsState()
ChildWidget(parentData = parentData)
}
Run Code Online (Sandbox Code Playgroud)
在可组合项内部ParentScreen,我有一个ChildWidget可组合项,它有自己的ViewModel …
android kotlin android-jetpack-compose dagger-hilt navigation-compose
我在我的应用程序中使用 Navigation-Compose :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeTheme {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = Screens.Dashboard.title) {
composable(Screens.Dashboard.title) {
DashboardScreen(navController)
}
composable(
Screens.Section.title, arguments = listOf(
navArgument(LINK) {
type = AssetParamType()
}
)
) {
SectionDetailsScreen(navController)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在每个屏幕上都有一个单独的应用程序栏,例如:
@Composable
fun DashboardScreen(
navController: NavHostController,
viewModel: DashboardViewModel = hiltViewModel()
) {
Scaffold(
topBar = {
TopAppBar(
title = {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize()
) {
Text(text = stringResource(id …Run Code Online (Sandbox Code Playgroud) 我试图将音频文件位置(/storage/emulated/0/Android/media/...)作为字符串从第一个屏幕传递home_screen到第二个屏幕detail_screen,当我正常传递上面的字符串作为参数时,我收到此错误
java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/detail_screen/45193/'/storage/emulated/0/Android/media/com.Slack/Notifications/Slack - Here you go.mp3' } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x442b361f) route=home_screen}
Run Code Online (Sandbox Code Playgroud)
至于这个地方,如果我传递像这样的字符串Here you go.mp3,它工作得很好!
我认为 Compose Navigation 无法区分/
android android-navigation android-architecture-navigation android-jetpack-compose navigation-compose
我正在使用 Jetpack Compose 和 Android 导航组件。当我在带有 的屏幕上时AlertDialog,我无法返回。我猜这是由于AlertDialog捕获了后退按钮事件。但是我不知道如何连接AlertDialog到导航组件?有没有官方的方法或最佳实践来做到这一点?这是我的代码:
// sample with a screen and a "navigate to dialog" button.
// once the button is pressed, an AlertDialog is shown.
// Using the back button while the AlertDialog is open has no effect ):
@Composable
fun MyNavHost(navController: NavHostController, modifier: Modifier = Modifier) {
NavHost(
navController = navController,
startDestination = "start_route",
modifier = modifier
) {
composable("start_route") {
Text("start screen")
}
// this is my screen with …Run Code Online (Sandbox Code Playgroud) android android-architecture-navigation android-jetpack-compose jetpack-compose-navigation navigation-compose