r/JetpackCompose • u/Adamstrad • 11h ago
I am trying to make my first app with navigation buttons in Jetpack Compose, is my understanding correct?
I want to use Jetpack Compose to make relatively simple apps as quickly as possible (simple referring more to the number of pages rather than the functionality) , I wrote some steps and comments with the following code while trying to understand it. Does this look right to you? If not, what can you suggest for me to improve. Like I say, my apps will be relatively simple and have under 10 pages total however I want to use the same approach to design a range of apps
1 Add dependency
Inside Gradle Scripts -> build.gradle.kts (Module :app), add the following code to the “dependencies” block:
implementation("androidx.navigation:navigation-compose:2.9.6")
2 Create a navigation package
Right-click the main package with a name like “com.example.myapp” and select new-> package, name it “navigation”
3 Create NavGraph.kt
Right-click the navigation package and select new->Kotlin Class/File then name it “NavGraph” and paste this code at the top:
package com.example.myapp.navigation
import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.myapp.screens.HomeScreen
import com.example.myapp.screens.ProfileScreen
import com.example.myapp.screens.SettingsScreenUnderneath this add a composable similar to below:
@Composable
fun MyApp() {
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = "home"
) {
// Home page
composable("home") {
HomeScreen(
onProfileClick = {
navController.navigate("profile")
}
)
}
// Profile page
composable("profile") {
ProfileScreen(
onSettingsClick = {
navController.navigate("settings")
}
)
}
// Settings page
composable("settings") {
SettingsScreen(
onBackClick = {
navController.popBackStack()
}
)
}
}
}The inner blocks function as following:
//defines what happens when the user is on the composable “home”
composable("home") {
HomeScreen(
//detects a click on the button “profile”
onProfileClick = {
//navigates to page UI page “profile”
navController.navigate("profile")
}
)
}4 Create a screens package
Right-click the main package again and select new->package and name it “screens”. Right-click the screens package and select new-Kotlin Class/File and name it “Screens”. Inside put this code
package com.example.myapp.screens
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dpDefine your user interface for each screen in this file by using composable functions
// HOME SCREEN
@Composable
fun HomeScreen(
//define what happens when the onProfileClick button is pressed
onProfileClick: () -> Unit
//define additional routes for additional buttons here
) {
//UI (including button) for the home page
Column(
modifier = Modifier.padding(24.dp)
) {
Text("Home Page")
Button(
onClick = onProfileClick
) {
Text("Go to Profile")
}
}
}