Skip to content

๐Ÿ”™ Back Navigation

Overview

Back navigation in zMantra follows a single-activity, fragment-back-stack approach.

  • All forward navigations push the fragment onto the back stack.
  • Pressing Back pops the stack until the root (e.g., Landing page).
  • When no fragments remain, the app exits.

Back can be triggered via:

  • System Back button / gesture
  • Toolbar Up arrow
  • In-UI โ€œGo Backโ€ buttons
  • Programmatic back after certain flows

How Itโ€™s Wired

Main Activity toolbar + Up navigation

File: app/src/main/java/com/zendalona/zmantra/MainActivity.kt

  • Listens for back stack changes to toggle the toolbar Up arrow:
supportFragmentManager.addOnBackStackChangedListener {
    val canGoBack = supportFragmentManager.backStackEntryCount > 0
    supportActionBar?.setDisplayHomeAsUpEnabled(canGoBack)
}
  • Delegates toolbar Up โ†’ Back:
override fun onSupportNavigateUp(): Boolean {
    onBackPressed()
    return true
}

Always use the MainActivity helper to ensure proper back stack behavior:

(requireActivity() as MainActivity)
    .loadFragment(MyNextFragment(), MainActivity.TRANSIT_FRAGMENT_OPEN)

This applies: .replace(...).addToBackStack(null).commit()


Default Back behavior

  • Pops the current fragment from the stack.
  • If stack is empty (e.g., LandingPageFragment), Back exits the app.

Fragment-Specific Handling

Score result screen

File: core/utility/common/ScorePageFragment.kt

  • Intercepts Back with a lifecycle-aware callback:
requireActivity().onBackPressedDispatcher.addCallback(
    this,
    object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            goBack() // cleanup + pop
        }
    }
)
  • Also handles toolbar Home (Up) in:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        goBack()
        return true
    }
    return super.onOptionsItemSelected(item)
}

Hint & User Guide screens

Files:

  • presentation/features/hint/HintFragment.kt
  • presentation/features/userguide/UserGuideFragment.kt

In-UI โ€œGo Backโ€ button โ†’

requireActivity().onBackPressedDispatcher.onBackPressed()

Auto-back after completing a flow

  • Drawing game โ†’ auto-return after announcing completion:
requireActivity().onBackPressedDispatcher.onBackPressed()
  • EndGame utility โ†’ on score completion:
EndScore.endGameWithScore(... onComplete = {
    activity.onBackPressedDispatcher.onBackPressed()
})

Accessibility

  • Toolbar Up arrow has a spoken label:
supportActionBar?.setHomeActionContentDescription(R.string.back_button_label)

Guidelines for New Screens

โœ… Always navigate via MainActivity.loadFragment(...) โœ… Register a back callback if custom cleanup is required โœ… Use in-UI Back buttons sparingly, but wire them via:

backButton.setOnClickListener {
    requireActivity().onBackPressedDispatcher.onBackPressed()
}

โœ… For fragments with toolbar menus, handle android.R.id.home locally