title: Separate Back Stack for each tab in BottomNavigationView Android using Fragments
body: <p>I'm implementing <code>BottomNavigationView</code> for navigation in an Android app. I am using Fragments to set the content for each tab.</p>
  
  <p>I know how to set up one fragment for each tab and then switch fragments when a tab is clicked. But how can I have a separate back stack for each tab?
  Here is the code to set up one fragment:</p>
  
  <pre><code>Fragment selectedFragment = ItemsFragment.newInstance();
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  transaction.replace(R.id.content, selectedFragment);
  transaction.commit();
  </code></pre>
  
  <p>For an example, <code>Fragment</code> A and B would be under Tab 1 and <code>Fragment</code> C and D under Tab 2. When the app is started Fragment A is shown and Tab 1 is selected. Then <code>Fragment</code> A might be replaced with Fragment B. When Tab 2 is selected Fragment C should be displayed. If Tab 1 is then selected <code>Fragment</code> B should once again be displayed. At this point, it should be possible to use the back button to show Fragment A.</p>
  
  <p>And Here is the code to set up next <code>fragment</code> in the same tab:</p>
  
  <pre><code>Fragment selectedFragment = ItemsFragment.newInstance();
  FragmentTransaction ft = getFragmentManager().beginTransaction();
  ft.replace(R.id.content, selectedFragment);
  ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  ft.addToBackStack(null);
  ft.commit();
  </code></pre>