Skip to content

Instantly share code, notes, and snippets.

@ImaginativeShohag
Last active June 23, 2024 14:41
Show Gist options
  • Save ImaginativeShohag/350674f91dc67ff72b0c7852f24a508d to your computer and use it in GitHub Desktop.
Save ImaginativeShohag/350674f91dc67ff72b0c7852f24a508d to your computer and use it in GitHub Desktop.
"MainActivity.kt" example indicates that calling `navigateUp()` multiple times will recreate the `Activity`. To solve the black issue 2 extension function given in "ExtNavController.kt" file.
import android.app.Activity
import android.content.Context
import androidx.lifecycle.Lifecycle
import androidx.navigation.NavController
/**
* Attempts to pop the controller's back stack.
* If the back stack is empty, it will finish the activity.
*
* @param context Activity context.
*/
fun NavController.popBackStackOrFinish(context: Context) {
if (!popBackStack()) {
(context as Activity).finish()
}
}
/**
* Attempts to pop the controller's back stack.
* It will check the current lifecycle and only allow the pop
* if the current state is RESUMED.
*
* See [reference](https://github.com/google/accompanist/issues/1408#issuecomment-1673011548)
*/
fun NavController.popBackStackOrIgnore() {
if (currentBackStackEntry?.lifecycle?.currentState == Lifecycle.State.RESUMED) {
popBackStack()
}
}
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.myapplication3.ui.theme.MyApplication3Theme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
println("destination: onCreate called")
setContent {
MyApplication3Theme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
val navController = rememberNavController()
LaunchedEffect(Unit) {
navController.addOnDestinationChangedListener { _, destination, _ ->
println("destination: $destination")
val extras = intent?.extras
if (extras?.getIntArray(NavController.KEY_DEEP_LINK_IDS) != null) {
println("destination: yes")
}
}
}
NavHost(
navController,
startDestination = "screen_c",
modifier = Modifier.padding(innerPadding),
) {
composable("screen_c") {
ScreenC(
navigateToA = {
navController.navigate("screen_a") {
popUpTo(navController.graph.findStartDestination().id) {
inclusive = true
}
}
},
)
}
composable("screen_a") {
ScreenA(
goToNext = {
navController.navigate("screen_b")
},
)
}
composable("screen_b") {
ScreenB(
goBack = {
val x = navController.navigateUp()
val y = navController.navigateUp()
println("destination: x: $x, y: $y")
},
)
}
}
}
}
}
}
}
@Composable
fun ScreenA(
modifier: Modifier = Modifier,
goToNext: () -> Unit,
) {
Button(onClick = { goToNext() }) {
Text("Next ->")
}
}
@Composable
fun ScreenB(
modifier: Modifier = Modifier,
goBack: () -> Unit,
) {
Button(onClick = { goBack() }) {
Text("<- Back")
}
}
@Composable
fun ScreenC(
modifier: Modifier = Modifier,
navigateToA: () -> Unit,
) {
Button(onClick = { navigateToA() }) {
Text("New")
}
}
// Logcat Output:
//
// destination: onCreate called <----
// destination: Destination(0x2ac563dd) route=screen_c
// destination: Destination(0x2ac563db) route=screen_a
// destination: Destination(0x2ac563dc) route=screen_b
// destination: Destination(0x2ac563db) route=screen_a
// destination: x: true, y: true
// destination: onCreate called <----
// destination: Destination(0x2ac563dd) route=screen_c
// destination: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment