Obtenha o objeto de fragmento atual
No meumain.xml
Eu tenh
<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 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();
Está definindo diferentes tipos de objetos Fragment FragmentType2,FragmentType3,...
) em horário diferente. Agora, em algum momento, preciso identificar qual objeto está lá.
Dentrocurt Eu 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);
TitleFragment titleFragmentByTag = (TitleFragment) fragmentManager.findFragmentByTag("fragmentTag");
Mas ambos os objetos (titleFragmentById e titleFragmentByTag) sãonull
Perdi algo
Estou a usarCompatibility Package, r3
e desenvolvendo paraAPI level 7
.
findFragmentById()
efindFragmentByTag()
funcionará se tivermos definido fragmento usandofragmentTransaction.replace
oufragmentTransaction.add
, mas iráreturn null
se tivermos definido o objeto em xml (como o que fiz no meumain.xml
). Acho que estou perdendo alguma coisa nos meus arquivos XML.