En mi main.xmltengo
<FrameLayout
android:id="@+id/frameTitle"
android:padding="5dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@drawable/title_bg">
<fragment
android:name="com.fragment.TitleFragment"
android:id="@+id/fragmentTag"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Y estoy configurando un fragmento de objeto como este
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new FragmentType1();
fragmentTransaction.replace(R.id.frameTitle, casinodetailFragment, "fragmentTag");
// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Está configurando diferentes tipos de objetos Fragment ( FragmentType2,FragmentType3,...) en diferentes momentos. Ahora en algún momento necesito identificar qué objeto está actualmente allí.
En resumen , necesito hacer algo como esto:
Fragment currentFragment = //what is the way to get current fragment object in FrameLayout R.id.frameTitle
Probé lo siguiente
TitleFragment titleFragmentById = (TitleFragment) fragmentManager.findFragmentById(R.id.frameTitle);
y
TitleFragment titleFragmentByTag = (TitleFragment) fragmentManager.findFragmentByTag("fragmentTag");
Pero ambos objetos (titleFragmentById y titleFragmentByTag) sonnull
¿Me perdí algo?
Estoy usando Compatibility Package, r3y desarrollando para API level 7.
findFragmentById()y findFragmentByTag()funcionará si hemos establecido un fragmento usando fragmentTransaction.replaceo fragmentTransaction.add, pero funcionará return nullsi hemos establecido el objeto en xml (como lo que hice en mi main.xml). Creo que me falta algo en mis archivos XML.