Instantly share code, notes, and snippets.
Created
June 2, 2022 18:51
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Debdutta-Panda/5d5d4a872a056a1c18f1f5ddcfb49f36 to your computer and use it in GitHub Desktop.
Pass arguments in jetpack compose navigation
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.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