Last active
October 27, 2024 12:57
-
-
Save vyachin/d84c457f2fc6ed4e9ce67d2f3b57541f to your computer and use it in GitHub Desktop.
Jetpack Compose Navigation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.app | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.activity.enableEdgeToEdge | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.Scaffold | |
import androidx.compose.material3.Text | |
import androidx.compose.ui.Modifier | |
import androidx.navigation.compose.NavHost | |
import androidx.navigation.compose.composable | |
import androidx.navigation.compose.rememberNavController | |
class MainActivity : ComponentActivity() { | |
private companion object Routes { | |
const val ROUTE_HOME = "home" | |
const val ROUTE_PROFILE = "profile" | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContent { | |
val navController = rememberNavController() | |
Scaffold { innerPadding -> | |
NavHost( | |
navController = navController, | |
startDestination = ROUTE_HOME, | |
modifier = Modifier.padding(innerPadding) | |
) { | |
composable(ROUTE_HOME, content = { | |
Button( | |
onClick = { | |
navController.navigate(ROUTE_PROFILE) | |
} | |
) { | |
Text("Go Profile") | |
} | |
}) | |
composable(ROUTE_PROFILE, content = { | |
Button( | |
onClick = { | |
navController.popBackStack() | |
} | |
) { | |
Text("Go Back") | |
} | |
}) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment