0

I'm trying to implement a dialog that confirms the user wants to destroy an activity. Looking at a few other questions, it seems I can override the onBackPressed() functionality to add this dialog. However, my app has navigation between fragments in the same activity so it is possible that the Back button is navigating between fragments and not actually going to destroy the activity. Is my approach going in the right direction or is it fundamentally flawed? If it is idiomatic, the next question is how to determine if the activity will be destroyed or not?

    class GameActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_game)
            val navView: BottomNavigationView = findViewById(R.id.game_nav_view)

            val navHostFragment = supportFragmentManager.findFragmentById(R.id.game_nav_host_fragment) as NavHostFragment
            val navController = navHostFragment.navController
            val appBarConfiguration = AppBarConfiguration(setOf(
                    R.id.navigation_fragment_a, R.id.navigation_fragment_b))
            setupActionBarWithNavController(navController, appBarConfiguration)
            navView.setupWithNavController(navController)
        }

        override fun onBackPressed() {
            val builder = AlertDialog.Builder(this)
                    .setTitle(R.string.game_activity_dialog_exit_game_title)
                    .setMessage(R.string.game_activity_dialog_exit_game_message)
                    .setCancelable(false)
                    .setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int ->
                        super.onBackPressed()
                    }
                    .setNegativeButton(android.R.string.cancel) { _: DialogInterface, _: Int -> }
            val alertDialog = builder.create()
            alertDialog.show()
        }
    }
Daniel Bank
  • 3,581
  • 3
  • 39
  • 50
  • Have you tried [checking if an activity is the last one in the activity stack for an application?](https://stackoverflow.com/questions/5975811/how-to-check-if-an-activity-is-the-last-one-in-the-activity-stack-for-an-applica) – javdromero Mar 03 '21 at 19:58
  • Yes this is a dupe of the above question as it turns out. – Daniel Bank Mar 05 '21 at 06:45
  • Actually, I'm not sure it is...you'd want to check if the *Fragment* is the last one in the stack, right? – Ryan M Mar 06 '21 at 00:02
  • Alright, I'll be honest. I got around my problem using `NavUtils.navigateUpFromSameTask(this);` which suffices for my use case. I don't care to preserve the history of fragments, I can just navigate back to the last activity and that's fine. Not sure what I should do with this question. – Daniel Bank Mar 06 '21 at 00:06

1 Answers1

0

Instead of handling back button pressing in your activity, you could do it in your fragments like this:

activity?.onBackPressedDispatcher?.addCallBack(viewLifecycleOwner, true){
            //Navigate to other fragment
        }

just implement Fragment ktx dependency

implementation "androidx.fragment:fragment-ktx:1.2.5"

For further information, check out this

AbrahamCuautle
  • 149
  • 1
  • 4