I have two fragment classes and two buttons in the application bar - each button is doing one of the following:
- check if a
fragmentfrom the other classes exists - if exists, replace the container with new instance of his
fragmentclass - if not exists create new instance of his
fragmentclass
Here is the sample code:
private void openReader() {
FragmentManager fragmentManager = getFragmentManager();
ArticleListFragment fragmentList = (ArticleListFragment) fragmentManager.findFragmentById(R.id.action_list);
ArticleReaderFragment fragmentReader = (ArticleReaderFragment) fragmentManager.findFragmentById(R.id.action_reader);
if (fragmentReader==null || ! fragmentReader.isInLayout()) {
if (fragmentList==null || ! fragmentList.isInLayout()) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.container_id, new ArticleReaderFragment());
//ft.addToBackStack(null);
ft.commit();
} else {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container_id, new ArticleReaderFragment());
//ft.addToBackStack(null);
ft.commit();
}
}
else {
// fragment.update(...);
}
}
and here is the activity XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/welcome_page">
<FrameLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container_id">
</FrameLayout>
</LinearLayout>
The issue is that the replace function is not destroying the old fragment class - it is adding more and more fragments over each other as the buttons are clicked.
Could anyone tell what I am doing wrong?
Aucun commentaire:
Enregistrer un commentaire