Android UI How to - Add fragment to back stack








When an activity goes into the background, the activity is placed in the back stack. This allows the activity to be resumed when the user presses the Back button.

Fragments are not automatically placed in the back stack when they go into the background.

Example

To place a fragment into the back stack, you need to explicitly call the addToBackStack() method during a fragment transaction.

//get the current display info
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(android.R.id.content, fragment1);
//add to the back stack
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();