I have 5 fragments.On pressing back button I want to move to home fragment no matter how many fragments are opened.
If I move from
[1] --> [2] and Press back [1] shown home fragment
[1] --> [3] and Press back [1] shown home fragment
[1] --> [4] and Press back [1] shown home fragment
[1] --> [5] and Press back [1] shown home fragment
As I want No Problem.
But Problem is
[1] --> [2] --> [3] and Press back home fragment overlap on [3]
[1] --> [2] --> [4] and Press back home fragment overlap on [4]
[1] --> [2] --> [5] and Press back home fragment overlap on [5]
I have added only home fragment to backStack and used replace fragment method.
Why this is happening???
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.all_activity_layout); setToolbar(); addFragment(AttractionsFragment.newInstance()); setNavigationDrawer(); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.nav_attractions: if (mCurrentNavigationDrawerItem != 0) { mCurrentNavigationDrawerItem = 0; replaceFragment(AttractionsFragment.newInstance()); } break; case R.id.nav_packages: if (mCurrentNavigationDrawerItem != 1) { mCurrentNavigationDrawerItem = 1; replaceFragment(PackagesFragment.newInstance()); } break; case R.id.nav_passes: if (mCurrentNavigationDrawerItem != 2) { mCurrentNavigationDrawerItem = 2; replaceFragment(PassesFragment.newInstance()); } break; case R.id.nav_coupons: if (mCurrentNavigationDrawerItem != 3) { mCurrentNavigationDrawerItem = 3; replaceFragment(CouponsFragment.newInstance()); } break; case R.id.nav_more: if (mCurrentNavigationDrawerItem != 4) { mCurrentNavigationDrawerItem = 4; replaceFragment(MoreFragment.newInstance()); } break; } mNavigationView.getMenu().getItem(mCurrentNavigationDrawerItem).setChecked(true); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } public void replaceFragment(Fragment fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Get Current Visible fragment Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container); // Add to back stack only if it is AttractionsFragment if (f instanceof AttractionsFragment) { transaction.addToBackStack(fragment.getClass().getName()); } transaction.replace(R.id.fragment_container, fragment); transaction.commit(); Log.d("Navigation", "BackStack Count:" + getSupportFragmentManager().getBackStackEntryCount()); } public void addFragment(Fragment fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.fragment_container, fragment, "AttractionsFragment"); transaction.commit(); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } }
Remove the
transaction.addToBackStack(fragment.getClass().getName());
line it will work fine. Please check the code below