title: Fragment's onDestroyView called but view is not being destroyed
body: <p>The Android fragment lifecycle shows that when a fragment is added to the backstack and then removed/replaced, <code>onDestroyView()</code> is called, and later on, when the fragment returns to the layout from the backstack, <code>onCreateView()</code> is called.</p>
  
  <p>From my understanding it means that the fragment's view is being destroyed and recreated. If the user has input text in an <code>EditText</code> in fragment A and goes to fragment B and then back to A, when the fragment comes back the <code>EditText</code>'s contents will have been erased.</p>
  
  <p>However, this is not happening in the following code; can anybody explain why? I have already verified that <code>FragmentA</code>'s <code>onDestroyView()</code> is being called.</p>
  
  <p><img src="https://i.stack.imgur.com/sWK9i.png" alt="enter image description here"></p>
  
  <h2>MainActivity.java</h2>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      private Fragment currentFragment = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if (savedInstanceState == null) {
              addFragment(new FragmentA());
          }
      }
  
      public void setCurrentFragment(Fragment fragment) {
          this.currentFragment = fragment;
      }
  
      @Override
      public void onBackPressed() {
          if (currentFragment instanceof FragmentB) {
              getSupportFragmentManager().popBackStackImmediate();
          } else {
              super.onBackPressed();
          }
      }
  
      public void addFragment(Fragment fragment) {
          getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).addToBackStack(null).commit();
          setCurrentFragment(fragment);
      }
  }
  </code></pre>
  
  <h2>FragmentA.java</h2>
  
  <pre><code>public class FragmentA extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_a, container, false);
  
          final EditText editText = (EditText)view.findViewById(R.id.editText);
  
          Button button = (Button)view.findViewById(R.id.button);
          button.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  FragmentB fragmentB = new FragmentB();
                  Bundle arguments = new Bundle();
                  arguments.putString("text", editText.getText().toString());
                  fragmentB.setArguments(arguments);
                  ((MainActivity)getActivity()).addFragment(fragmentB);
              }
          });
          return view;
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          Log.d("Tag", "FragmentA.onDestroyView() has been called.");
      }
  }
  </code></pre>
  
  <h2>FragmentB.java</h2>
  
  <pre><code>public class FragmentB extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_b, container, false);
          TextView textView = (TextView)view.findViewById(R.id.textView);
          textView.setText(getArguments().getString("text"));
          return view;
      }
  }
  </code></pre>
  
  <h2>activity_main.xml</h2>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.fragmenttest.MainActivity"
      tools:ignore="MergeRootFrame" /&gt;
  </code></pre>
  
  <h2>fragment_a.xml</h2>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;EditText
          android:id="@+id/editText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/&gt;
  
      &lt;Button
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <h2>fragment_b.xml</h2>
  
  <pre><code>&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/textView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  </code></pre>