Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe navController.previousBackStackEntry as a state #121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/src/main/java/com/example/cupcake/CupcakeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.example.cupcake

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import androidx.annotation.StringRes
Expand All @@ -40,6 +41,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraph
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand Down Expand Up @@ -103,11 +105,17 @@ fun CupcakeApp(
backStackEntry?.destination?.route ?: CupcakeScreen.Start.name
)

@SuppressLint("RestrictedApi")
val backStack by navController.currentBackStack.collectAsState()
val previousBackStackEntry = backStack.asReversed().asSequence().drop(1).firstOrNull { entry ->
entry.destination !is NavGraph
}

Scaffold(
topBar = {
CupcakeAppBar(
currentScreen = currentScreen,
canNavigateBack = navController.previousBackStackEntry != null,
canNavigateBack = previousBackStackEntry != null,
navigateUp = { navController.navigateUp() }
)
}
Expand Down