Nel mio main.xmlho
<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 sto impostando un frammento come questo
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new FragmentType1();
fragmentTransaction.replace(R.id.frameTitle, casinodetailFragment, "fragmentTag");
// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Sta impostando diversi tipi di oggetti Fragment ( FragmentType2,FragmentType3,...) in momenti diversi. Ora ad un certo punto ho bisogno di identificare quale oggetto è attualmente lì.
In breve, devo fare qualcosa del genere:
Fragment currentFragment = //what is the way to get current fragment object in FrameLayout R.id.frameTitle
Ho provato quanto segue
TitleFragment titleFragmentById = (TitleFragment) fragmentManager.findFragmentById(R.id.frameTitle);
e
TitleFragment titleFragmentByTag = (TitleFragment) fragmentManager.findFragmentByTag("fragmentTag");
Ma entrambi gli oggetti (titleFragmentById e titleFragmentByTag)null
mi sono perso qualcosa?
Sto usando Compatibility Package, r3e sviluppando per API level 7.
findFragmentById()e findFragmentByTag()funzionerà se abbiamo impostato il frammento usando fragmentTransaction.replaceo fragmentTransaction.add, ma funzionerà return nullse abbiamo impostato l'oggetto su xml (come quello che ho fatto nel mio main.xml). Penso che mi manchi qualcosa nei miei file XML.