义齿


https://developer.android.com/jetpack/compose/navigation?hl=ko#deeplinks

build.gradle

dependencies {
    def nav_version = "2.4.1"

    implementation("androidx.navigation:navigation-compose:$nav_version")
}

NavHost

  • まずNavHostをNavControllerと組み合わせる  NavHost  組合せに関連付ける必要があります. NavHostは、構成可能なターゲットを指定するためのナビゲーション図を提供する.  接続NavController.
    構成可能なターゲットは、コンビネーション間で移動できる必要があります.グループ間の移動  NavHostの内容は自動的に  再構成.ナビゲーション図面の各構成可能なターゲット パスに関連付けられます.
  • NavHost(navController = navController, startDestination = "profile") {
        composable("profile") { Profile(/*...*/) }
        composable("friendslist") { FriendsList(/*...*/) }
        /*...*/
    }
  • ナビゲーション移動は、従来のjetpackナビゲーションと同様にnavControllerである.navideを使用してナビゲートします.現在に移動すると、合成は、操作に対応するidを追加するのではなく、ターゲットを決定します.
  • @Composable
    fun Profile(navController: NavController) {
        /*...*/
        Button(onClick = { navController.navigate("friendslist") }) {
            Text(text = "Navigate next")
        }
        /*...*/
    }
  • を移動してナビゲートするときに使用するbundleのようにパラメータを渡すことができます.
  • 
    /**인자를 보내줄 떄 **/
    navController.navigate("profile/user1234")
    
    /**인자를 받을 때 **/
    NavHost(startDestination = "profile/{userId}") {
        ...
        composable("profile/{userId}") {...}
    }
    
    NavHost(startDestination = "profile/{userId}") {
        ...
        composable(
            "profile/{userId}",
            arguments = listOf(navArgument("userId") { type = NavType.StringType })
        ) {...}
    }

    NavController

    NavControllerは、アプリケーション画面および各画面状態を構成するコンポーネントのスタックを追跡する状態プールであるNavigationコンポーネントの中心APIである.
    グループ内  rememberNavController()  使用方法  NavControllerを作成できます.
    val navController = rememberNavController()

    分割NavigGraph

  • グラフィックが大きいほど、グラフィックを複数の方法に分割することが推奨され、分割することができる.
  • fun NavGraphBuilder.loginGraph(navController: NavController) {
        navigation(startDestination = "username", route = "login") {
            composable("username") { ... }
            composable("password") { ... }
            composable("registration") { ... }
        }
    }
  • で分割されたグラフィックを整理します.
  • NavHost(navController, startDestination = "home") {
        communityGraph(navController)
        loginGraph(navController)
        setttingGraph(navController)
    }

    BottomNavigation

  • は、まず、Bottom Navigationの上位構造を表示するNavControllerを定義し、次に基準ナビゲーション項目を配置する.
  • の下部ナビゲーションメニューの項目を定義します.
  • sealed class Screen(val route: String, @StringRes val resourceId: Int) {
        object Profile : Screen("profile", R.string.profile)
        object FriendsList : Screen("friendslist", R.string.friends_list)
    }
    
    val items = listOf(
       Screen.Profile,
       Screen.FriendsList,
    )
  • BottomNavigationは、その存在する上位構造において定義される.
  • BottomNavigation  グループ内  currentBackStackEntryAsState()  関数の現在の使用  NavBackStackEntryを取得します.このトピックで現在  NavDestinationにアクセスします.では. ネストされたナビゲーションを使用している場合は、必ず  hierarchy  ヘルパーメソッドを使用して、プロジェクトのパスと現在のターゲットとその親ターゲットのパスを比較します.  BottomNavigationItemの選択したステータスを表示できます.
  • 💡 `NavBackStackEntry'はバックグラウンドスタック内のアイテムを表すデバイスです.
    このオブジェクトが提供するライフサイクル、viewModel、SaveStateは、バックグラウンドスタック内のターゲットのライフサイクルと同じです.
    val navController = rememberNavController()
    Scaffold(
      bottomBar = {
        BottomNavigation {
          val navBackStackEntry by navController.currentBackStackEntryAsState()
          val currentDestination = navBackStackEntry?.destination
          items.forEach { screen ->
            BottomNavigationItem(
              icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
              label = { Text(stringResource(screen.resourceId)) },
              selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
              onClick = {
                navController.navigate(screen.route) {
                  // Pop up to the start destination of the graph to
                  // avoid building up a large stack of destinations
                  // on the back stack as users select items
                  popUpTo(navController.graph.findStartDestination().id) {
                    saveState = true
                  }
                  // Avoid multiple copies of the same destination when
                  // reselecting the same item
                  launchSingleTop = true
                  // Restore state when reselecting a previously selected item
                  restoreState = true
                }
              }
            )
          }
        }
      }
    ) { innerPadding ->
      NavHost(navController, startDestination = Screen.Profile.route, Modifier.padding(innerPadding)) {
        composable(Screen.Profile.route) { Profile(navController) }
        composable(Screen.FriendsList.route) { FriendsList(navController) }
      }
    }