Go back to previous screen by clicking on bottom navigation bar item

108 Views Asked by At

Using Navigation Component, Bottom Navigation Bar XML. I set up a navigation graph in which I nested another navigation graph called Home and set it to the first tab at the bottom navigation, if I go to the next screen in that tab and click on the tab button nothing happens and I would like a popBackStack or return to the root of the tab child nav graph

I tried setOnItemSelectedListener, but it messes up the setupWithNavController and is impossible to use, I couldn't find any other solutions.

1

There are 1 best solutions below

0
Dinesh Meda On
suppose if you want to go back ontwo times back press then you have to write like this




override fun onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed()
            return
        }
        doubleBackToExitPressedOnce = true
        Toast.makeText(this, "${getString(R.string.backButton)}", Toast.LENGTH_SHORT).show()

      Handler(Looper.getMainLooper()).postDelayed(Runnable {
            doubleBackToExitPressedOnce = false
        }, 2000)
    }