Problemas con Android Fragment back stack

Tengo un problema masivo con la forma en que parece funcionar el backstack del fragmento de Android y estaría muy agradecido por la ayuda que se ofrece.

Imagina que tienes 3 fragmentos

[1] [2] [3]

Quiero que el usuario pueda navegar[1] > [2] > [3] Pero en el camino de vuelta (presionando el botón atrás)[3] > [1].

Como me hubiera imaginado esto se lograría no llamandoaddToBackStack(..) Al crear la transacción que trae el fragmento.[2] en el titular del fragmento definido en XML.

La realidad de esto parece que si no quiero.[2] para volver a aparecer cuando el usuario presiona el botón Atrás en[3], No debo llamaraddToBackStack en la transacción que muestra fragmento[3]. Esto parece completamente contrario a la intuición (quizás provenga del mundo iOS).

De todos modos si lo hago de esta manera, cuando salgo de[1] > [2] y presione de nuevo llego de nuevo a[1] como se esperaba.

Si voy[1] > [2] > [3] y luego presione de nuevo salto a[1] (como se esperaba). Ahora el comportamiento extraño ocurre cuando intento saltar a[2] de nuevo desde[1]. Ante todo[3] se muestra brevemente antes[2] aparece a la vista Si presiono de nuevo en este punto[3] aparece, y si vuelvo a presionar la aplicación, se cierra la aplicación.

¿Alguien puede ayudarme a entender qué está pasando aquí?


Y aquí está el archivo XML de diseño para mi actividad principal:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >

<fragment
        android:id="@+id/headerFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.fragment_test.FragmentControls" >
    <!-- Preview: layout=@layout/details -->
</fragment>
<FrameLayout
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"

        />



Actualizar Este es el código que estoy usando para construir por jerarquía nav

    Fragment frag;
    FragmentTransaction transaction;


    //Create The first fragment [1], add it to the view, BUT Dont add the transaction to the backstack
    frag = new Fragment1();

    transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.detailFragment, frag);
    transaction.commit();

    //Create the second [2] fragment, add it to the view and add the transaction that replaces the first fragment to the backstack
    frag = new Fragment2();

    transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.detailFragment, frag);
    transaction.addToBackStack(null);
    transaction.commit();


    //Create third fragment, Dont add this transaction to the backstack, because we dont want to go back to [2] 
    frag = new Fragment3();
    transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.detailFragment, frag);
    transaction.commit();


     //END OF SETUP CODE-------------------------
    //NOW:
    //Press back once and then issue the following code:
    frag = new Fragment2();
    transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.detailFragment, frag);
    transaction.addToBackStack(null);
    transaction.commit();

    //Now press back again and you end up at fragment [3] not [1]

Muchas gracias

Respuestas a la pregunta(8)

Su respuesta a la pregunta