ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Achieve drawer behaviour like in Google apps #72

Closed azurh closed 7 years ago

azurh commented 7 years ago

In the NavigationDrawer sample, clicks on list items in the navigation drawer are handled like this:

switch (item.getItemId()) {
    case R.id.bb_menu_recents:
        mNavController.switchTab(INDEX_RECENTS);
        break;
    case R.id.bb_menu_favorites:
        mNavController.switchTab(INDEX_FAVORITES);
        break;
    case R.id.bb_menu_nearby:
        mNavController.switchTab(INDEX_NEARBY);
        break;
    case R.id.bb_menu_friends:
        mNavController.switchTab(INDEX_FRIENDS);
        break;
    case R.id.bb_menu_food:
        mNavController.switchTab(INDEX_FOOD);
        break;
}

That means that clicking on any navigation drawer item will clear the backstack, and a new back press will exit the application. This feels a bit unnatural. I also checked Gmail, Play Store and Google Fit for that behaviour, and the first item in the nav drawer (i.e. the first fragment which is shown when starting the app) is kept in the backstack until the end. This means that the first fragment will be the last one the user will see just before hitting the back button before the app exit, no matter what fragments they have visited.

I tried with FragNavController.pushFragment() instead of FragNavController.switchTab() but without success.

How can we achieve this behaviour? (keep the first fragment in the backstack).

Thanks.

ncapdevi commented 7 years ago

It doesn't clear the backstack, it switches to whichever backstack you've selected. If what you want is a single stack, then you can use this library with a stack size equal to one, or you could just use the standard Android Fragment manager since the value of this library is in managing multiple fragment stacks.