Fragments do not disappear [duplicate]

0

Good night, I'm creating an app to train and it contains side menu that each menu item calls a Fragment but 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 not make the disappearance of the first fragment .... I looked for some solution on the internet, and there is a user here that asked the same question, I tried to follow the same procedure but I could not ... Can anyone help me?

code xml:

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

code java:

@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;
    }

Thank you ....

    
asked by anonymous 22.07.2017 / 02:33

1 answer

1

Try changing your XML by:

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

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

    
22.07.2017 / 03:57