Android: Fragmentos backStack
Estoy tratando de cargar un nuevo fragmento cuando se llama a un método. Este método crea un nuevo fragmento y "reemplaza" al otro fragmento:
private void showTestFragment(Fragment oldFragment, boolean addBackStack, BaseAdapter adapter, int position) {
Cursor cursor = (Cursor)adapter.getItem(position);
if(cursor != null){
int idx = cursor.getColumnIndexOrThrow(Episode._ID);
long rowId = cursor.getLong(idx);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(oldFragment != null){
Log.i(TAG, "Removing the old fragment");
fragmentTransaction.remove(oldFragment);
}
TestFragment testFragment = new TestFragment();
testFragment.setId(rowId);
fragmentTransaction.add(android.R.id.content, testFragment);
if(addBackStack){
Log.i(TAG, "Added to the backstack");
fragmentTransaction.addToBackStack(TAG);
}
fragmentTransaction.commit();
Fragment f = getFragmentManager()
.findFragmentById(R.id.index);
Log.i(TAG, "after commit, frag is "+ f);
}
}
Esto funciona bien, hasta que vuelva. El último fragmento, debe ser eliminado cuando regrese. Antes voy a implementar métodos sobre las actividades.
public void onBackPressed(){}
para eliminar el último fragmento, quiero saber si manejo el cambio de fragmento correctamente. Parece que me estoy perdiendo algo aquí ..