5

I am working on an application that has three fragments which are defined in an XML file:

 [HeaderFragment] 

 [MainFragment] 

 [FooterFragment]

The first screen initiates the three fragments, the Header- and FooterFragment are static so will not change content.

The MainFragment is initial a menu with buttons and a transparant background (MenuFragment). When I click an item in the menu I replace the MenuFragment with a new fragment (DetailsFragment) like this:

FragmentTransaction transaction = mFragmentManager.beginTransaction();
Fragment newFragment = new DetailFragment();
transaction.replace(R.id.content_container, newFragment);
transaction.addToBackStack(newFragment.getTag());
transaction.commit();

The DetailFragment shows up and when I press back, the MenuFragment appears and everything works how it should.

Here is my problem:

Inside my DetailFragment, I have a toggle option to filter the content, this is a button. When this is clicked, the DetailFragmentFiltered replaces the DetailFragment on the same way as the code above states. The only difference is that I don't add it to the BackStack because after filtering and pressing Back.. I still want to return to the MenuFragment.

When I clicked the filter button and press Back, the DetailFragment (or DetailFragmentFiltered) is shown behind my MenuFragment. Afcourse, I don't want this.

DroidBender
  • 7,762
  • 4
  • 28
  • 37

1 Answers1

4

Make sure you don't use a static fragment relation to an XML by setting the first fragment as "android:name" in the layout.

Make with framelayouts the layout of the XML and add the fragments flexibly as shown in this tutorial:

http://developer.android.com/training/basics/fragments/fragment-ui.html

Boy
  • 7,010
  • 4
  • 54
  • 68