cnsdH
cnsdH

Reputation: 33

One fragment is being drawn on top of another

I have 2 fragments, One for a game and one for the score. When I'm moving from the game fragment to the score fragment it works great, but when I'm trying to move back to the game it draws both fragments on the same container (even when using the remove function).

        public void nextLvl(Fragment current, Fragment next, String score, String nextTag, int LvlTime){
        Bundle args = new Bundle();
        args.putString("scoreKey", score);
        args.putInt("levelTime", LvlTime);
        next.setArguments(args);

        FragmentManager fm = getFragmentManager();
        if (fm != null){
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(current);
            ft.add(R.id.fragment_place, next, nextTag);
            ft.commit();
            Log.d("FM","MOVED TO "+next.toString());
        }else{
            Log.d("FM","fm is null");
        }
  }

This the code I use to move between fragments.

Here's an image to demonstrate what I mean in "being drawn on top of each other":

https://i.sstatic.net/0yrrb.png

1 = Game fragment.

2 = Score fragment.

3 = Result of moving from Score fragment to Game fragment.

Upvotes: 0

Views: 785

Answers (2)

patrick
patrick

Reputation: 71

Use replace fragment instead of add

example:

 FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_place, next, nextTag);
        ft.commit();

Upvotes: 1

Furedal
Furedal

Reputation: 533

Try using ft.replace instead of remove() and add()

example:

ft.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();

Upvotes: 0

Related Questions