0

I have a problem with my fragments. I use this code to navigate between the fragments:

Between the "main" fragments (without backstack, because I want the user to exit when he presses back (it works)):

    FragmentManager fragmentManager = getFragmentManager();
    final FragmentTransaction ft = fragmentManager.beginTransaction();

    ft.setCustomAnimations(R.anim.slide_in_up_honeycomb, R.anim.slide_out_up_honeycomb);
    ft.replace(R.id.container, NewsFragment.newInstance(position + 1), NewsFragment.class.getSimpleName());
    ft.commit();

and between the "inner" fragments (with backstack):

            FragmentTransaction ft = ((Activity) getActivity()).getFragmentManager().beginTransaction();
            Fragment nextFragment = LexikonDetailFragment
                    .newInstance(item);

            ft.setCustomAnimations(R.anim.slide_in_up_honeycomb, R.anim.slide_out_up_honeycomb);
            ft.replace(R.id.container, nextFragment);
            ft.addToBackStack(LexikonDetailFragment.class.getSimpleName());
            ft.commit();

But in the following case:

fragment A -> fragment A1

fragment A1 -> fragment B

Press back button (should end the app) -> goes back to fragment A1

This happens:

Image

It looks like the A1 fragment doesnt gets removed from the backstack and stays in the background. I thought one possible solution could be to set to all fragments a white background..but this wouldn't fix the problem, it would just hide it. So what could be a possible solution?

fractalwrench
  • 4,028
  • 7
  • 33
  • 49
Daniel
  • 753
  • 9
  • 15
  • Do you know the meaning of addToBackStack? – tiny sunlight Jan 17 '16 at 10:55
  • Sure, but i thought the fragment gets automatically removed when "back" is pressed. – Daniel Jan 17 '16 at 12:07
  • I want the inner fragments to be reuseable, thats why i use addtobackstack. But as soon as a upper fragment is changed i dont want to revive the old inner fragments again. – Daniel Jan 17 '16 at 12:29
  • So, you can add a background to LexikonDetailFragment.Or use ft.hide(R.id.container, oldFragment); before add newFragment.addToBackStack is just like intent from one Activity to another Activity.You can see the pre Activity if the second Activity has a transparent theme. – tiny sunlight Jan 17 '16 at 12:35
  • Hm. I understand your intent example, but this shouldn't be a problem if i use the ft.replace function. Anywas, i found a solution. Ill update it later :) Thanks for your help! – Daniel Jan 17 '16 at 13:00

1 Answers1

0

Okay, i found a solution:

I had to remove the inner fragments manualy via the command:

fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

I call this command each time my "outer" fragment changes. The inner ones get removed and tada...it works like a charm :)

Daniel
  • 753
  • 9
  • 15