Fragments does not disappear

Asked

Viewed 76 times

0

Good evening, I am creating an app to train and in it contains side menu that each menu item calls a Fragment only that I’m in trouble, because when I click on an item it appears the called Fragment, but when I click on the other item the second Fragment is superimposed on the first, so I can’t make the disappearance of the first Fragment.... I looked for a solution on the Internet, and there’s a user here from the world who asked the same question, I tried to follow the same procedure but I couldn’t... Can someone help me?

xml code:

<fragment
    android:id="@+id/fragments"
    android:name="com.nathan.lotogera.lotogera.Fragments.DuplaSena"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

java code:

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

            DuplaSena duplaSena = new DuplaSena();

            fragmentTransaction.replace(R.id.fragments, duplaSena, duplaSena.getTag());
            fragmentTransaction.commit();


        } else if (id == R.id.nav_slideshow) {
            Quina quina = new Quina();

            fragmentTransaction.replace(R.id.fragments, quina, quina.getTag());
            fragmentTransaction.commit();


        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

inserir a descrição da imagem aqui

Thank you....

1 answer

1


Try trading your XML for:

<FrameLayout
       android:id="@+id/fragments"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

The element fragment in XML is to load a single fragment. To replace fragments, you have to use another container, like the one I put above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.