Previous snippet visible in new snippet release

I created an Android app with a navigation drawer and when I click on an item from the nav drawer the old fragment is not replaced, I can still see the old fragment under the new fragment. How do I fix this problem?

private void displaySelectedScreen(int id)
    {
        Fragment fragment = null;

        switch (id)
        {
            case R.id.home:
                fragment = new Main();
                break;
            case R.id.settings:
                Intent intent = new Intent(this, Settings.class);
                startActivity(intent);
                break;
            case R.id.about:
                Intent intent1 = new Intent(this, About.class);
                startActivity(intent1);
                break;
            case R.id.share:
                try
                {
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType("text/plain");
                    i.putExtra(Intent.EXTRA_SUBJECT, "Audiophileradio");
                    String sAux = "\nLet me recommend you this application\n\n";
                    sAux = sAux + "https://play.google.com/store/apps/details?id=Orion.Soft \n\n";
                    i.putExtra(Intent.EXTRA_TEXT, sAux);
                    startActivity(Intent.createChooser(i, "choose one"));
                }
                catch(Exception e)
                {
                    //e.toString();
                }
                break;
            case R.id.send:
                fragment = new Feedback();
                break;
        }
        if (fragment != null)
        {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.contentFrame, fragment).commit();
        }

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

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        displaySelectedScreen(id);

        return true;
    }

      

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="audiophileradio.example.com.audiophileradio.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:background="@drawable/backg1">

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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="13dp"
        android:layout_height="21dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="+"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.977"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.13"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"/>

      

Picture

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                         xmlns:app="http://schemas.android.com/apk/res-auto"
                                         xmlns:tools="http://schemas.android.com/tools"
                                         android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="Title :"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.103"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.081"/>

<EditText
    android:id="@+id/editText"
    android:layout_width="218dp"
    android:layout_height="35dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.593"
    app:layout_constraintVertical_bias="0.084"
    android:background="@drawable/border"/>

      

enter image description here

+3


source to share


2 answers


You can add tag for snippet

getSupportFragmentManager().beginTransaction()
.add(R.id.frame, new Main()(), "content_fragment")
.commit();

      

Find it using a tag



getSupportFragmentManager().findFragmentByTag("content_fragment");

      

Add or remove

        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag("content_fragment");
        if(fragment == null)
        {
            // Create the detail fragment and add it to the activity using a fragment transaction.
            mainFragment = new Main();
            fragmentManager.beginTransaction()
                    .add(R.id.contentFrame, mainFragment,"content_fragment")
                    .commit();
        }
        else
        {
            // get old fragment
            mainFragment = (Main)fragment;
        }

      

0


source


You can add a background to either the fragment or the contentFrame:



<FrameLayout
    android:id="@+id/contentFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">

      

0


source







All Articles