Last active
June 23, 2024 14:41
-
-
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.
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
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