No meu main.xmleu tenho
<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>
E eu estou definindo um objeto de fragmento 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();
Ele está definindo diferentes tipos de objetos Fragment ( FragmentType2,FragmentType3,...) em momentos diferentes. Agora, em algum momento, preciso identificar qual objeto está atualmente lá.
Em resumo , preciso fazer algo assim:
Fragment currentFragment = //what is the way to get current fragment object in FrameLayout R.id.frameTitle
Eu tentei o seguinte
TitleFragment titleFragmentById = (TitleFragment) fragmentManager.findFragmentById(R.id.frameTitle);
e
TitleFragment titleFragmentByTag = (TitleFragment) fragmentManager.findFragmentByTag("fragmentTag");
Mas ambos os objetos (titleFragmentById e titleFragmentByTag) são Perdinull
alguma coisa?
Estou usando Compatibility Package, r3e desenvolvendo para API level 7.
findFragmentById()e findFragmentByTag()funcionará se tivermos definido o fragmento usando fragmentTransaction.replaceor fragmentTransaction.add, mas funcionará return nullse tivermos definido o objeto em xml (como o que fiz no meu main.xml). Acho que estou perdendo alguma coisa nos meus arquivos XML.