2012-08-09 10 views
13

Estoy usando el paquete de soporte v4.No se puede encontrar el fragmento por la etiqueta

mFragmentManager = getSupportFragmentManager(); 

FragmentTransaction ft = mFragmentManager.beginTransaction(); 
ft.add(R.id.fragContainer1, new ModelListFragment(), FRAG_MODEL_LIST); 
ft.add(R.id.fragContainer2, new TrimListFragment(), FRAG_TRIM_LIST); 
ft.commit(); 

Fragment fragment = mFragmentManager.findFragmentByTag(
      MainActivity.FRAG_MODEL_LIST); 
Log.d("MY", "found fragment: " + (fragment != null)); 

Siempre devuelve "found fragment: false". Me falta algo realmente obvio aquí, ¿qué es?

Respuesta

15

Como se puede ver en doc:

la confirmación no ocurre de inmediato; se programará como trabajo en el hilo principal a realizar la próxima vez que el hilo esté listo.

es por eso que no ve su fragmento justo después de commit().

+14

Y si _realmente_ desea que se aplique la transacción, entonces use [executePendingTransactions] (http://developer.android.com/reference/android/app/FragmentManager.html#executePendingTransactions()) –

Cuestiones relacionadas