Fix back stack

This commit is contained in:
2025-12-07 17:23:46 +03:00
Unverified
parent 30a20f85b8
commit fc9a25ad29
4 changed files with 42 additions and 25 deletions
+1
View File
@@ -37,6 +37,7 @@ kotlin {
implementation(libs.constraintlayout)
implementation(compose.materialIconsExtended)
implementation(libs.jetbrains.kotlinx.coroutines.core)
implementation(libs.navigation.compose)
}
androidMain.dependencies {
@@ -0,0 +1,38 @@
package com.pr0gramm3r101.utils
import androidx.navigation.NavController
/**
* Navigates to the specified route and clears the entire back stack.
* This ensures the destination becomes the new root of the navigation stack.
*
* @param route The destination route to navigate to
* @param launchSingleTop If true, prevents multiple instances of the same destination
*/
fun NavController.navigateAndWipeBackStack(route: String, launchSingleTop: Boolean = true) {
// First, pop all previous entries (keeping current screen for now)
while (previousBackStackEntry != null) {
if (!popBackStack()) {
break
}
}
// Get current route to pop it with the navigation
val currentRoute = currentBackStackEntry?.destination?.route
// Navigate to the new route, removing the current screen as well
if (currentRoute != null && currentRoute != route) {
navigate(route) {
popUpTo(currentRoute) {
inclusive = true
}
this.launchSingleTop = launchSingleTop
}
} else {
// Fallback: just navigate if current route is same or null
navigate(route) {
this.launchSingleTop = launchSingleTop
}
}
}