Search code examples
androidandroid-fragmentsandroid-viewpagerkotlinfragmentmanager

Android: Duplicating fragments in FragmentManager


EDIT: I want to solve problem of duplicating fragment in view Pager, removing them isn't the best solution.

I have a problem with FragmentManager in my app. Every time when I click "Wykłady teoretyczne" or "Wykłady popularnonaukowe" on Navigation Drawer my app is creating new fragment ScheduleFragment() which contains ViewPager and in SchedulePagerAdapter app is creating new 3 fragments (ScheduleEventFragment()) to swipe them in ScheduleFragment. My problem is that FragmentManager doesn't remove old ScheduleEventFragments, for all the time it keeps them in memory. How can I fix this problem?

I have my project on GitHub, please check it.

There you have screen from debuging Debuging: Fragment Manager contains old fragments


Solution

  • FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.remove(R.id.container,fragment,optionalTag); fragmentTransaction.commit();You can also detach or replace