title: Invoking a fragment from another fragment and then replacing its layout
body: <p>I am rather new to android development and have a problem with the invocation/replacement of fragments. My issue is very similar to this one: <a href="https://stackoverflow.com/questions/14810348/android-fragment-replace-doesnt-replace-content-puts-it-on-top">Android - fragment .replace() doesn&#39;t replace content - puts it on top</a>. The "solution" to this was however, that the fragment had to be created in the code and not in the xml-file.</p>
  
  <p>I now have the same problem but my fragment is created dynamically in the code (MainActivity.java):</p>
  
  <pre><code>public void selectDrawerItem(MenuItem menuItem) {
  
      Fragment fragment = null;
      Class fragmentClass;
      switch(menuItem.getItemId()) {
          case R.id.nav_first_fragment:
              fragmentClass = FirstFragment.class;
              break;
          case R.id.nav_second_fragment:
              fragmentClass = SecondFragment.class;
              break;
          case R.id.nav_third_fragment:
              fragmentClass = ThirdFragment.class;
              break;
          default:
              fragmentClass = FirstFragment.class;
      }
  
      try {
          fragment = (Fragment) fragmentClass.newInstance();
      } catch (InstantiationException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e){
          e.printStackTrace();
      }
  
      FragmentManager fragmentManager = getSupportFragmentManager();
      fragmentManager.beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
  
      menuItem.setChecked(true);
      setTitle(menuItem.getTitle());
  
      mDrawer.closeDrawers();
  }
  </code></pre>
  
  <p>Now when i try to let that Fragment invoke a different Fragment and replace its own content with that of the new one, it just adds its content below the already existing one instead of replacing it.</p>
  
  <p>My FirstFragment.java:</p>
  
  <pre><code>public class FirstFragment extends Fragment {
  
  private View v;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      v = inflater.inflate(R.layout.content_first,container, false);
      configureImageButton();
      return v;
  }
  
  private void configureImageButton() {
      // TODO Auto-generated method stub
      ImageButton btn = (ImageButton) v.findViewById(R.id.imageButton1);
  
      btn.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();
  
              Fragment f1 = null;
              Class c;
              c = FirstFirstFragment.class;
  
              try {
                  f1 = (Fragment) c.newInstance();
              } catch (IllegalAccessException e){
                  e.printStackTrace();
              } catch (java.lang.InstantiationException e){
                  e.printStackTrace();            }
  
  
              FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
              fragmentTransaction.replace(R.id.tableLayout1, f1);
              fragmentTransaction.addToBackStack(null);
              fragmentTransaction.commit();
  
          }
      });
  
  }
  </code></pre>
  
  <p>And my FirstFirstFragment.java:</p>
  
  <pre><code>public class FirstFirstFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.content_first_first, container, false);
      return view;
  }
  </code></pre>
  
  <p>My activity_main.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!-- This DrawerLayout has two children at the root  --&gt;
  &lt;android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;!-- This LinearLayout represents the contents of the screen  --&gt;
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
          &lt;!-- The ActionBar displayed at the top --&gt;
          &lt;include
              layout="@layout/toolbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
  
          &lt;!-- The main content view where fragments are loaded --&gt;
          &lt;FrameLayout
              android:id="@+id/flContent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              android:layout_width="match_parent"
              android:layout_height="match_parent" &gt;
  
              &lt;Switch
                  android:id="@+id/switch1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/&gt;
          &lt;/FrameLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;!-- The navigation drawer that comes from the left --&gt;
      &lt;!-- Note that `android:layout_gravity` needs to be set to 'start' --&gt;
      &lt;android.support.design.widget.NavigationView
          android:id="@+id/nvView"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:background="@android:color/white"
          app:menu="@menu/drawer_view"
          app:headerLayout="@layout/nav_header"/&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>I read in the answers of the article i linked at the beginning that someone had this exact problem and solved it by using the same FragmentManager. Just like me, he had used getSupportFragmentManager() in his Activity and getFragmentManager() in his Fragment before.</p>
  
  <p>But the Fragment does not know getSupportFragmentManager(), so how is that supposed to work?</p>
  
  <p>I tried everything i know of to resolve this issue but nothing worked. Then i read this </p>
  
  <blockquote>
    <p>As far as i know you cannot add fragments into fragments.In design document it's written that "A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle." So your design seems like impossible to develop for now.</p>
  </blockquote>
  
  <p>on a similar question on stackoverflow and am now wondering: <strong>Is it even possible to let a fragment invoke another fragment which then replaces the first one?</strong>(and not just adds it below)</p>
  
  <p>I really hope this question is reasonable and not some stupid beginner's mistake because i searched the whole internet and did not find a proper answer.</p>
  
  <p>Thanks in advance for your kind answers :).</p>