Navigation drawer fragments overlap even with replace()
<p>I have a navigation drawer in an activity. The activity's layout has a relative layout for a fragment. The id of relativelayout is <code>mainContent</code> in which I load the fragment(call it fragA) in the <code>onCreate()</code> of the activity. I have this same fragment also in the navigation drawer along with other navigation items. It is loaded oncreate of activity and can also be loaded from navigation drawer.</p>

<p>On selecting the navigation items, I replace the fragment in the <code>mainContent</code> which is the relativelayout in the activity with <code>replace()</code>. In the navigation drawer, the 0th item is the "fragB"(the same as fragA but in nav drawer). Second is another fragment(fragC). When I select fragB "more than once" and then select fragA and press back button, fragB and fragC overlap. </p>

<p>And also I have to keep pressing back button to go to initial screen because every time I select an item from nav drawer, new fragments fragBs and fragCs are created. Not replaced. I replace them in <code>mainContent</code>(first relative layout) in the activity's layout. I'm using <code>FrameLayout</code> for the fragments. </p>

<p>This is the activity's layout in which I place fragments:</p>

<pre><code>&lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.prematixsofs.taxiapp.DateVehiclePicker"&gt;
&lt;!-- The main content view --&gt;

&lt;RelativeLayout
    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;

    &lt;include
        layout="@layout/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal|bottom"&gt;&lt;/include&gt;
&lt;/RelativeLayout&gt;

&lt;!-- The navigation drawer --&gt;
&lt;RelativeLayout
    android:id="@+id/drawerPane"
    android:layout_width="230dp"
    android:layout_height="match_parent"

    android:layout_gravity="start"&gt;

    &lt;!-- Profile Box --&gt;

    &lt;RelativeLayout
        android:id="@+id/profileBox"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#ff02c7c4"
        android:padding="8dp"&gt;

        &lt;ImageView
            android:id="@+id/avatar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/user" /&gt;

        &lt;LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/avatar"
            android:orientation="vertical"&gt;

            &lt;TextView
                android:id="@+id/userName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:textColor="#fff"
                android:textSize="16sp"
                android:textStyle="bold" /&gt;

            &lt;TextView
                android:id="@+id/viewProfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginTop="4dp"
                android:text="View Profile"
                android:textColor="#fff"
                android:textSize="12sp" /&gt;
        &lt;/LinearLayout&gt;
    &lt;/RelativeLayout&gt;

    &lt;!-- List of Actions (pages) --&gt;
    &lt;ListView
        android:id="@+id/navList"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_below="@+id/profileBox"
        android:background="#ffffffff"
        android:choiceMode="singleChoice" /&gt;

    &lt;TextView
        android:id="@+id/invisibleTextViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" /&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p></p>

<p>This is the Activity's code where I load the fragA:</p>

<pre><code> android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.mainContent, new DateTimePicker(),"tags").commit();
</code></pre>

<p>This is nav drawer onSelect:</p>

<pre><code>  private void selectItemFromDrawer(int position) {
    Fragment fragment = null;

    switch (position){
        case 0: fragment = new DateTimePicker();
            break;
        case 1: fragment = new PreferencesFragment();
            break;
        case 2:
            mDrawerLayout.closeDrawer(mDrawerPane);
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(DateVehiclePicker.this);

            alertDialog.setTitle("Logout");

            // Setting Dialog Message
            alertDialog.setMessage("Do you want to Logout?");
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    sessionManager.logoutUser();
                }
            });

            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("EXIT", true);
                    startActivity(intent);
                    dialog.cancel();
                }
            });

            alertDialog.show();
    }
    if(fragment!=null)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.addToBackStack(null).replace(R.id.mainContent, fragment);
        fragmentTransaction.commit();
        mDrawerLayout.closeDrawer(mDrawerPane);
    }
</code></pre>

<p>This is where I set listener for nav drawer:</p>

<pre><code> mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            selectItemFromDrawer(position);

        }
    });
</code></pre>

<p>I call the method <code>selectItemFromDrawer(position);</code></p>