Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back key to pop the Fragment shows overlapping fragments

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();
    } 
like image 897
FHan Avatar asked Jun 23 '11 15:06

FHan


People also ask

How to check If DialogFragment is showing?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

Can a fragment be reused in multiple activities?

Yes you can, but you have to add more logic to your fragments and add some interfaces for each activity.


2 Answers

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?.

like image 179
PJL Avatar answered Oct 11 '22 05:10

PJL


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

like image 34
Deepak Goel Avatar answered Oct 11 '22 04:10

Deepak Goel