How to remove all fragments except first one?

2.1k Views Asked by At

My application contains AppBaseActivity having NavigationView with few menu items. By default, I load Home fragment & on clicking each menu item from drawer, I open specific fragment.

My problem is, I need keep Home fragment all the time showing if user clicks back button.

Stepwise explanation :

  1. On activity launch, loads Home fragment by default
  2. Suppose selects Menu Item 1, loads related fragment[*4]
  3. Suppose selects Menu Item 2, loads related fragment[*4]
  4. I want to make back stack clear however, keeping Home fragment persistent so that if user presses back button instead of opting menu item from drawer or simply go to any fragment & on killing it, should navigate back to Home fragment.

In my current case, it simply closes/terminates my app.

AppBaseActivity Java (some part of code)

    onCreate() {
    fragmentTransaction = fragmentManager.beginTransaction();
            HomeFragment homeFragment = new HomeFragment();
            fragmentTransaction.add(R.id.body_container, homeFragment, getResources().getString(R.string.app_name));
            fragmentTransaction.commit();
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        navigationView.getMenu().findItem(item.getItemId()).setChecked(true);

        switch (item.getItemId()) {

            case R.id.nav_terms :
                fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                fragmentTransaction = fragmentManager.beginTransaction();
                TCFragment tcFragment = new TCFragment();
                fragmentTransaction.add(R.id.body_container, tcFragment, getResources().getString(R.string.tc_screen_name));
                fragmentTransaction.commit();
                break;

            case R.id.nav_about_us :
                fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                fragmentTransaction = fragmentManager.beginTransaction();
                AboutUsFragment aboutUsFragment = new AboutUsFragment();
                fragmentTransaction.add(R.id.body_container, aboutUsFragment, getResources().getString(R.string.about_us_screen_name));
                fragmentTransaction.commit();
                break;
        }

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

There are 2 best solutions below

3
On

try by adding this line with home fragment before commit:

fragmentTransaction addTobackStack(null);
4
On

You need to add the transaction to the backstack using addToBackStack(). The back press would automatically pop the topmost fragment from the backstack.

Refer to https://developer.android.com/guide/components/fragments.html

The relevant section is "Performing Fragment Transactions"