Merge pull request #4384 from kiwix/Fixes#4383

Fixed: Pressing the back button did not close the application when there were no fragments in the back stack.
This commit is contained in:
Kelson 2025-08-14 12:29:23 +02:00 committed by GitHub
commit 0e39ce6f9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 9 deletions

View File

@ -482,7 +482,7 @@ abstract class CoreReaderFragment :
mainActivityBottomAppBarScrollBehaviour = (requireActivity() as CoreMainActivity).bottomAppBarScrollBehaviour,
documentSections = documentSections,
showTableOfContentDrawer = shouldTableOfContentDrawer,
onUserBackPressed = { onUserBackPressed(requireActivity() as CoreMainActivity) },
onUserBackPressed = { onUserBackPressed(activity as? CoreMainActivity) },
navHostController = (requireActivity() as CoreMainActivity).navController
)
DialogHost(alertDialogShower as AlertDialogShower)
@ -852,12 +852,10 @@ abstract class CoreReaderFragment :
}
@Suppress("ReturnCount", "NestedBlockDepth", "LongMethod", "CyclomaticComplexMethod")
private fun onUserBackPressed(coreMainActivity: CoreMainActivity): FragmentActivityExtensions.Super {
private fun onUserBackPressed(coreMainActivity: CoreMainActivity?): FragmentActivityExtensions.Super {
when {
coreMainActivity.leftDrawerState.isOpen -> {
coreMainActivity.uiCoroutineScope.launch {
coreMainActivity.leftDrawerState.close()
}
coreMainActivity?.navigationDrawerIsOpen() == true -> {
coreMainActivity.closeNavigationDrawer()
return FragmentActivityExtensions.Super.ShouldNotCall
}
@ -909,8 +907,8 @@ abstract class CoreReaderFragment :
isHomePageOfServiceWorkerZimFiles(url, webViewBackWordHistoryList)
) {
// If it is the last page that is showing to the user, then exit the application.
if (coreMainActivity.navController.previousBackStackEntry?.destination?.route !=
coreMainActivity.searchFragmentRoute
if (coreMainActivity?.navController?.previousBackStackEntry?.destination?.route !=
coreMainActivity?.searchFragmentRoute
) {
activity?.finish()
}

View File

@ -262,10 +262,17 @@ fun OnBackPressed(
onUserBackPressed: () -> FragmentActivityExtensions.Super,
navHostController: NavHostController
) {
BackHandler(enabled = true) {
// Tracks whether the fragment's BackHandler should be enabled.
var shouldEnableBackPress by remember { mutableStateOf(true) }
BackHandler(enabled = shouldEnableBackPress) {
val result = onUserBackPressed()
if (result == FragmentActivityExtensions.Super.ShouldCall) {
// Disable the fragment's BackHandler so that MainActivity's back handler can be triggered.
shouldEnableBackPress = false
navHostController.popBackStack()
} else {
// Keep the fragment's BackHandler active.
shouldEnableBackPress = true
}
}
}