NavControllerとNavHost
アプリのナビゲーショングラフを定義します。
「NavControllerとNavHost」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Moving Between Screens
Real apps have many screens. Navigation Compose is the official library that lets you move between them without juggling fragments or activities. 🧭
Add the Dependency
Navigation lives in its own artifact. Add androidx.navigation:navigation-compose to your module's build file before you can use any of it.
dependencies {
implementation("androidx.navigation:navigation-compose:2.7.7")
}Meet the NavController
The NavController is the brain of navigation. It tracks where you are, remembers history, and carries out every move you ask for.
Remember the Controller
You create one with rememberNavController() so it survives recomposition. Make it once near the top of your app and pass it down.
val navController = rememberNavController()Meet the NavHost
The NavHost is the container that swaps screens in and out. It shows exactly one destination at a time, based on the controller.
Wire Host to Controller
A NavHost needs two things: the navController that drives it and a startDestination that says which screen to show first.
NavHost(
navController = navController,
startDestination = "home"
) { }The startDestination
The startDestination is the route shown when the app opens. It is also the screen users land on after the back stack empties.
Declaring Destinations
Inside the NavHost lambda you list your screens with composable("route"). Each one ties a route name to the UI it should show.
NavHost(navController, startDestination = "home") {
composable("home") { HomeScreen() }
composable("profile") { ProfileScreen() }
}Navigate by Route
To change screens, call navController.navigate("route"). The host sees the request and swaps in the matching destination instantly.
Button(onClick = { navController.navigate("profile") }) {
Text("Open profile")
}One Controller, One Host
Keep a single NavController paired with a single NavHost for an app's main flow. Sharing one source of truth avoids confusing, out-of-sync history.
Why Routes Are Strings
Routes are just String identifiers, like tiny URLs inside your app. This keeps destinations decoupled and easy to link later.
Quick Check
Which piece actually performs the screen change?
Recap
You met the duo behind navigation: the NavController drives moves and history, while the NavHost shows one destination at a time from your route list. 🎉
よくある質問
「NavControllerとNavHost」レッスンは無料ですか?
はい。「NavControllerとNavHost」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「NavControllerとNavHost」で何を学びますか?
アプリのナビゲーショングラフを定義します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「NavControllerとNavHost」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- NavControllerとNavHost
- Composableの遷移先とルート
- 画面間で引数を渡す
- バックスタックとディープリンク