9

I created a sample app to test this overlapping issue.

I have a fragment type, Fragment1, and I create a new instance of Fragment1 and add it to a FrameLayout in my activity at runtime. I add the fragment with the help of a few buttons.

Note: I have given each new instance of Fragment1 a different number(#1, #2, #3, etc.) to display on the UI to help me figure out which fragment I am viewing.

So.. here is what I do:

  1. Click on Button 3, create new instance of Fragment1 and add it to Frame1.
  2. Click on Button 4, create new instance of Fragment1 and add it to Frame1 and add it to the fragment backstack.
  3. Repeat 1 and 2.
  4. Repeat 1 and 2.

Now, I have fragments in this order: 1(#1),2(#2),1(#3),2(#4),1(#5),2(#6).

I press the back key when viewing fragment #6.

  1. Back key press, UI displays (#5).
  2. Back key press, UI displays (#3 AND #5),
  3. Back key press, UI displays (#1, #3, AND #5)

It seems fragments are getting displayed ON TOP of each other.

WHY? Is there an overlapping issue? How can I clear out this overlapping issue. I thought this would be an issue on the compatibility library... but it is also on 3.0.

Code for adding fragments:

public int  doFragmentChange(int cont1, Fragment frag1, String tag1, int cont2, Fragment frag2, String tag2, 
            boolean addToStack, String stackTag) {
        FragmentManager fm = getFragmentManager();// getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (frag1 != null) {
            ft.replace(cont1, frag1, tag1);
        }
        if (frag2 != null) {
            ft.replace(cont2, frag2, tag2);
        }
        // add fragment to stack
        if (addToStack)
            ft.addToBackStack(stackTag);
        return ft.commit();
    } 
FHan
  • 528
  • 2
  • 7
  • 18

2 Answers2

1

If you perform two add calls one after the other (two commit calls) then yes the fragments will appear overlaid, one on top of the other effectively.

So (for new example) if say you replace frag1 with frag2 and then frag3 with frag4 in the same frame with no backstack transaction then I would expect frag2 and frag4 to be overlaid.

Furtheremore there is also a potential issue in your chaining of replace. You should call a separate commit for each. See Android — Replace Fragment Back Stack With New Stack?.

Community
  • 1
  • 1
PJL
  • 18,735
  • 17
  • 71
  • 68
  • 1
    as i can see there is no two add calls per commit. beside i faced the same overlapping behavior. http://stackoverflow.com/questions/14269350/how-to-keep-only-first-initial-fragment-in-back-stak-fragment-overlapping – Arvis Jan 11 '13 at 00:23
1

Just override the onBackPress() or onKeyUp and remove the top fragment.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Deepak Goel
  • 5,624
  • 6
  • 39
  • 53