Search code examples
androidandroid-fragmentsfragmenttransactiononbackpressed

reset addToBackStack() on MainActivity


I am building an android application using default Navigation Drawer layout and Fragment to switch from one layout to another.

The layout sequence is like

Dashboard -> Settings -> Profile Settings

MailActivity.class loads main_activity.xml which containes a frameLayout to contain fragment

and Dashboard loads ActivityDashboard Fragment.

Now, when I switch from Dashboard to Settings and then Profile Settings. onBackPress works fine. But after on Profile Settings, I selected Dashboard from Navigation Drawer to go back to the home screen. Then pressing back key follows the stack

Dashboard -> Profile Settings -> Settings -> Dashboard

The code for Navigation Drawer is

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

// calling the method displaySelectedScreen()
displaySelectedScreen(item.getItemId());

return true;
}

private void displaySelectedScreen(int itemId) {
// creating Fragment object
Fragment fragment = null;

// initializing fragment
switch (itemId) {
    case R.id.nav_dashboard:
        fragment = new ActivityDashboard();
        break;
    case R.id.nav_calendar:
        fragment = new ActivityCalendar();
        break;
    case R.id.nav_history:
        fragment = new ActivityHistory();
        break;
    case R.id.nav_about:
        fragment = new ActivityAbout();
        break;
    case R.id.nav_settings:
        fragment = new ActivitySettings();
        break;
}

// replacing the fragment
if (fragment != null) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.addToBackStack(null);
    ft.commit();
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}

I'm using same FragmentTransaction code to switch from one fragment to another in ActivityDashboard, ActivitySettings fragment classes.

Q 1. How to flush the addBackToStack when the ActivityDashboard is loaded so that user have only option to exit app ?

Q 2. How to get back to ActivityDashboard, when layout is changed from Navigation Drawer irespective of which fragment is replaced ?

i.e., When user is on ActivitySettings fragment and changed to ActivityHistory from Navigation Drawer, then on backPress, user must be tracked following

ActivityHistory -> ActivityDashboard

instead of

ActivityHistory -> ActivitySettings -> ActivityDashboard

onBackPressed() method on MainActivity.class is

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }

}

Solution

  • Simply call

    fragmentManager.popBackStack(stackName /* null if you did not supply a stack name while creating your fragments */, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    

    inside your ActivityDashboard