blonsky95 / DigiCoachAndroid

0 stars 0 forks source link

with bottom bar nav - pressing back will take you to home tab before logging out #95

Closed blonsky95 closed 4 years ago

blonsky95 commented 4 years ago

https://stackoverflow.com/questions/43870485/how-to-handle-bottom-navigation-perfectly-with-back-pressed

blonsky95 commented 4 years ago

So when I navigate horizontally I add a backstack of horizontal_fragment, when I get a vertical, I add a vertical_fragment backstack.

situation A (current) When there is an horizontal slide, it gets rid of verticals If you press back with a vertical, it hides it horizontals have no add to backstack, but verticals do, when you press back you're calling on back pressed, and when you slide to the side, it checks if there is a verticalFragment, in which case, it pops backstack (gets rid of vertical) and does the horizontal slide

situation B (I want) Same as before but, pressing back button without vertical should take you home Home has no backstack, but everything else does. Issue is one thing is fragment transactions and the other is navbar selection toggles.

blonsky95 commented 4 years ago

Working solution!

facts

  1. There is a home fragment - which is like the root of navigation, and back presses always lead you here. When you back press once more in this fragment, you exit the app. 1 solution. On the HomeActivity, which hosts the fragments, onBackPressed checks the active fragment, if it is HomeFragment, it finishes the activity, aka, kills the app

  2. When user is in a non HomeFragment, pressing back should take them to Home Fragment, if a vertical fragment is displayed, just undisplay it, and if you change tabs while vertical is open, just hide it and make the horizontal change. 2 solution. When you open a non homefragment, you do two things: 1. you get rid of all entries in backstack 2, you add to backstack "horizontal_fragment": if (fragment !is HomeFragment) { supportFragmentManager.popBackStack( "horizontal_fragment", POP_BACK_STACK_INCLUSIVE ) transaction.addToBackStack("horizontal_fragment") }

When you open a vertical fragment you ad a "vertical_fragment" to backstack So when user presses back button, if the backstackcount is 1, it sets the nav bar to page_home, but if it is more than 1 (there is a vertical frag active) then it calls super.onBackPressed which will get rid of the last entry in backstack (the vertical fragment transaction) this then returns the count to 1, and if you press back button it will take you back home.

When I switch whilst a vertical is displayed, it gets rid the backstack anyway with the code seen above, so goodbye vertical_fragment, and starts resetted in the next tab

The reason I cant use super.onBackPressed to navigate back home with horizontal fragment, is because even though it gets rid of the transaction and takes me back to home fragment, the nav bar selected doesnt get updated.

Another thing, even if I dont go back to homefragment with my bar selected item forcing, and used super.onBackpressed, it still goes always to home fragment because in every transaction it knows what it is replacing for what, aka what fragment is removed and which one is added so it nows what it goes back to.


 override fun onBackPressed() {

        //if its home fragment that is active then quit
        if (supportFragmentManager.findFragmentById(R.id.fragment_container) is HomeFragment) {
            finish()
        }

        var x =supportFragmentManager.backStackEntryCount

        if (x>0) {
            //only horizontal fragment active = go to home
            if (x==1) {
                bottom_navigation.selectedItemId=R.id.page_home
            } else {
                //when its 2 or more and it has vertical fragment
                super.onBackPressed()
            }
        }
    }