mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Fixed: Pressing the back button did not close the application when there were no fragments in the back stack.
* The `BackHandler` of top-level destination fragments was always active, preventing the `MainActivity` handler from being called because the top-level fragment's back handler consumed the back event. As a result, when the back stack was empty, pressing the back button did not exit the application. * We now dynamically enable the `BackHandler` only while a fragment can handle the back event. Once the fragment has handled it, the handler is disabled so that `MainActivity` can process the back event.
This commit is contained in:
parent
17b148fc4f
commit
10357e78f4
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user