Fragment's onDestroyView llamado pero la vista no está siendo destruida

El ciclo de vida de los fragmentos Android muestra que cuando se agrega un fragmento al backstack y se elimina / reemplaza, onDestroyView() se llama y posteriormente, cuando el fragmento vuelve al diseño del backstack, se llama onCreateView() .

Desde mi entendimiento, significa que la vista del fragmento está siendo destruida y recreada. Si el usuario tiene texto de entrada en un EditText en el fragmento A y va al fragmento B y luego de nuevo a A, cuando el fragmento vuelve el contenido de EditText se habrá borrado.

Sin embargo, esto no está sucediendo en el código siguiente; ¿Alguien puede explicar por qué? Ya he verificado que onDestroyView() FragmentA está siendo llamado.

Introduzca aquí la descripción de la imagen

MainActivity.java

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

FragmentA.java

 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."); } } 

FragmentB.java

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

Activity_main.xml

 <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" /> 

Fragmento_a.xml

 <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"> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> 

Fragment_b.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" /> 

Los fragmentos de la pila posterior guardan su estado en un Bundle . Esto incluye el estado de jerarquía de vista con el contenido actual de EditText s y tal.

Para destruir el Fragmento .. Use este onDestroyView o en onDetach

  if (fragment != null) { fragment = new YourFragment(); FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction(); ft.remove(fragment); ft.commit(); } 
  • Fragmento en la ronda Desgaste reloj girando en negro en el emulador / reloj
  • IllegalStateException: DialogFragment no se puede adjuntar a una vista de contenedor - (en ActionBarActivity)
  • Reemplazar la barra de herramientas para cada fragmento de cajón de navegación
  • Android EditText siguiente foco
  • Cómo obtener fragmentos para ver correctamente
  • Cómo resolver un error: getSharedPreferences (String, int) no está definido para el tipo nuevo View.OnClickListener () {}
  • fragmentar dinámico fragmento android
  • La actividad de NavigationDrawer está llena de fragmentos de retorno de llamada y lógica de aplicaciones
  • Ocultar teclado al navegar de un fragmento a otro
  • No es posible establecer SwipeFlingAdapterView desde el adaptador de matriz
  • Fragmentos todavía agregados a backstack sin llamar addToBackStack, ¿por qué?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.