-
-
Save SanjayDevTech/30453873212dda25f1c9b182549f0165 to your computer and use it in GitHub Desktop.
Navigation using Intent Snippet - AndroidX Compose Navigation Series
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
@Composable | |
fun OuterComposable( | |
content: @Composable () -> Unit | |
) { | |
val context = LocalContext.current | |
Scaffold( | |
topBar = { | |
TopAppBar(title = { | |
Text(text = stringResource(id = context.applicationInfo.labelRes)) | |
}) | |
} | |
) { | |
Box(modifier = Modifier.padding(it)) { | |
content() | |
} | |
} | |
} | |
@Composable | |
fun FirstScreen(navigateToSecondScreen: () -> Unit) { | |
OuterComposable { | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text(text = "First Screen", fontSize = 48.sp) | |
Button(onClick = { | |
navigateToSecondScreen() | |
}) { | |
Text(text = "Navigate to 2nd Screen") | |
} | |
} | |
} | |
} | |
@Composable | |
fun SecondScreen() { | |
OuterComposable { | |
Column( | |
modifier = Modifier.fillMaxSize(), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text(text = "Second Screen", fontSize = 48.sp) | |
} | |
} | |
} |
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
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
AndroidXComposeNavigationTheme { | |
val context = LocalContext.current | |
FirstScreen { | |
val intent = Intent(context, SecondActivity::class.java) | |
startActivity(intent) | |
} | |
} | |
} | |
} | |
} |
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
class SecondActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
AndroidXComposeNavigationTheme { | |
SecondScreen() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment