Skip to content

Instantly share code, notes, and snippets.

@Debdutta-Panda
Created June 2, 2022 18:51
Show Gist options
  • Save Debdutta-Panda/5d5d4a872a056a1c18f1f5ddcfb49f36 to your computer and use it in GitHub Desktop.
Save Debdutta-Panda/5d5d4a872a056a1c18f1f5ddcfb49f36 to your computer and use it in GitHub Desktop.
Pass arguments in jetpack compose navigation
package com.debduttapanda.powernavigation
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.debduttapanda.powernavigation.ui.theme.PowerNavigationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PowerNavigationTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "page_a"){
composable(
"page_a"
){
PageA(navController)
}
composable(
"page_b/{money}",
arguments = listOf(
navArgument("money"){
type = NavType.IntType
}
)
){backStackEntry->
PageB(navController,backStackEntry.arguments?.getInt("money"),)
}
}
}
}
}
}
}
@Composable
fun PageA(navController: NavHostController) {
var money by remember { mutableStateOf(0) }
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
){
TextField(
value = money.toString(),
onValueChange = {
try {
money = it.toInt()
} catch (e: Exception) {
money = 0
}
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number
)
)
Text(
"Page A: Send Money",
color = Color(0xfff44336),
fontSize = 24.sp,
fontWeight = FontWeight.Bold
)
Button(
onClick = {
navController.navigate("page_b/$money")
},
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(0xfff44336),
contentColor = Color.White
)
) {
Text("Send Money")
}
}
}
@Composable
fun PageB(navController: NavHostController, receivedMoney: Int?) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
){
Text(
"Page B: received money $receivedMoney",
color = Color(0xfff44336),
fontSize = 24.sp,
fontWeight = FontWeight.Bold
)
Button(
onClick = {
navController.navigateUp()
},
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(0xfff44336),
contentColor = Color.White
)
) {
Text("Go back and pay me again")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment