Note that there are some explanatory texts on larger screens.

plurals
  1. POaddToBackStack displays all fragments
    primarykey
    data
    text
    <p>I'm using MainActivity to manage the display of several fragments. I have a showFragment method that iterates through my fragment array and either shows/hides them, and sets the backstack accordingly. The issue is that any time I press the back button, the HomeFrag gets displayed, with all of the other fragments piled up underneath. I was under the impression from the documentation that the back button will reverse the last transaction (anything that was added before the commit). Can anyone explain why this might be causing this unexpected behavior? Here is my main activity. The fragments are all defined in activity_main.xml:</p> <pre><code> public class MainActivity extends MenuBaseActivity { private static final int HOME = 0; private static final int WRITE = 1; private static final int UPLOAD = 2; private static final int SHARE = 3; private static final int SNAP = 4; private static final int POSITION = 5; private static final int FRAGMENT_COUNT = POSITION + 1; /** The current fragment being displayed **/ private int mCurrentFragment = -1; private Fragment[] fragments = new Fragment[FRAGMENT_COUNT]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getSupportFragmentManager(); fragments[HOME] = fm.findFragmentById(R.id.homeFrag); fragments[WRITE] = fm.findFragmentById(R.id.writeFrag); fragments[UPLOAD] = fm.findFragmentById(R.id.uploadFrag); fragments[SHARE] = fm.findFragmentById(R.id.shareFrag); fragments[SNAP] = fm.findFragmentById(R.id.snapFrag); fragments[POSITION] = fm.findFragmentById(R.id.positionFrag); // If no fragments are currently being displayed, show the home frag if(mCurrentFragment == -1) { showFragment(0,true); } } /* * Handles the click event for all navigation buttons (navOnClick) in the fragments. */ //TODO: Tag navigation with text strings instead of integers public void navOnClick(View v) { Object objectTag = v.getTag(); int tag = Integer.valueOf(objectTag.toString()); showFragment(tag, true); } public void showFragment(int fragmentIndex, boolean addToBackStack) { Log.v(Constants.getLogTag(), "Main: fragmentIndex:" + fragmentIndex + " backstack:" + addToBackStack); FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); //ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); for (int i = 0; i &lt; fragments.length; i++) { if (i == fragmentIndex) { ft.show(fragments[i]); } else { ft.hide(fragments[i]); } } if (addToBackStack) { Log.v(Constants.getLogTag(), "Main: Adding to backstack"); ft.addToBackStack(null); } //Update the current fragment identifier mCurrentFragment = fragmentIndex; ft.commit(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload