Obtenga el objeto de fragmento actual
En mimain.xml
Yo teng
<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í.
Incort Necesito hacer algo como esto:
Fragment currentFragment = //what is the way to get current fragment object in FrameLayout R.id.frameTitle
Intenté lo siguiente
TitleFragment titleFragmentById = (TitleFragment) fragmentManager.findFragmentById(R.id.frameTitle);
TitleFragment titleFragmentByTag = (TitleFragment) fragmentManager.findFragmentByTag("fragmentTag");
Peroboth los objetos (titleFragmentById y titleFragmentByTag) sonnull
¿Me he perdido algo
Estoy usandoCompatibility Package, r3
y desarrollando paraAPI level 7
.
findFragmentById()
yfindFragmentByTag()
funcionará si hemos establecido un fragmento usandofragmentTransaction.replace
ofragmentTransaction.add
, pero lo haráreturn null
si hemos establecido el objeto en xml (como lo que he hecho en mimain.xml
). Creo que me falta algo en mis archivos XML.