FragmentTransaction de ne rien faire

Je suis à l'apprentissage de fragments et donnée ci-dessous est mon premier fragment de programme. Un projet simple, où j'ai 2 écrans. Lorsque je clique sur le bouton suivant de premier écran, le deuxième bouton doit être affichée.

FragmentTransaction de ne rien faire

Je suis ciblant Android 2.1 et au-dessus et à l'aide de la compatibilité paquet

AppMainFragmentActivity.java

public class AppMainFragmentActivity extends FragmentActivity {
  @Override
  protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.app_main_layout);
  }
}

app_main_layout.xml

<?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" android:id="@+id/fragment_container">

   <fragment class="com.research.fragmentstudy.FirstFragment"     
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:id="@+id/id_first_fragment"/>

</LinearLayout>

FirstFragment.java

public class FirstFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.first_fragment_layout
                 , container,false);

     Button nextButton =(Button) view.findViewById(R.id.button);
     nextButton.setOnClickListener(nextListener);
     return view;
  }

  private OnClickListener nextListener  =   new OnClickListener() {
     @Override
     public void onClick(View v) {
        FragmentManager fm = ((AppMainFragmentActivity)FirstFragment.this
                           .getActivity()).getSupportFragmentManager();
        SecondFragment fragment = new SecondFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container, fragment);
        ft.commit();
     }
  };
}

first_fragment_layout.xml

<?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" android:id="@+id/first_fragment_root">

   <TextView android:layout_height="wrap_content" 
       android:layout_width="fill_parent"
       android:text="Fragment 1" android:gravity="center_horizontal" />

   <Button android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal"
       android:layout_width="wrap_content" android:id="@+id/button"
       android:text="Next" />

</LinearLayout>

SecondFragment.java

public class SecondFragment extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.second_fragment_layout,
                     container,false);
      return view;
   }
}

second_fragment_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:orientation="vertical" android:id="@+id/first_fragment_root">

   <TextView android:layout_height="wrap_content" 
      android:layout_width="fill_parent"
      android:text="Fragment 2" android:gravity="center_horizontal" />

</LinearLayout>

Eh bien, je suis le premier écran bien. Maintenant,

Ce que j'attendais, à partir de ma compréhension de fragment

  • Quand je clique sur le bouton Suivant dans l'écran 1, SecondFragment est créé,
    son onCreate() et onCreateView() est appelée.
  • SecondFragment est montré, et FirstFragment est détruit (depuis que je suis
    pas de l'ajouter à la backstack). Il n'y aura pas d'animation depuis
    par défaut fragment opération n'a pas d'animation.

Ce qui se passe

  • SecondFragment est se créé bien, son onCreate() et onCreateView() est appelée.
  • Mais FirstFragment reste sur l'écran, et une seconde de ne jamais montrer.

Maintenant ma compréhension de fragment peut être erronée. Mais je crois que quand on commit() un fragment de la transaction, le premier fragment doit être remplacé par seconde (la première obtient caché ou détruit). Eh bien rien ne semble se passer. Pourquoi est-ce? Doit-on détruire manuellement/masquer le premier fragment?

Note : je sais que c'est une longue question pour une chose fondamentale. Mais j'ai mis tout mon code car je ne suis pas sûr de l'endroit où j'ai tout fait foiré.

OriginalL'auteur Krishnabhadra | 2012-09-14