0

I have a strange "bug" in my app. Here is the construction: We have a NavigationDrawer with several Fragments (like every app does). Some fragments (e.g. Canteen-Frag) contain a ViewPager with a FragmentPagerAdapter. As everyone knows, the ViewPager is lazy and only created the visible fragment as well as his neighbours.

Now, here is th problem: If we open the Canteen-Frag, navigate back with the back button and open the News-Frag again, the visible Fragments as well as the neighbours are gone.

You can see the main view here: https://lh5.ggpht.com/0KJCniup8zvFk05QAtzeQpoClKZnaSzaPepgeLwsToPNmTGJnRRj31PLhumw_DGD2Q=h900-rw

And the canteen here: https://lh5.ggpht.com/81EnVDrlqONgE0V1i5UDw3c2cbEJcMZ0PLX0zB_syBeawHWQ5OfLtpeapZqAHRhZ2w=h900-rw

(both are the image links of the playstore of the "myHHU"-App)

Does everyone know, why the ViewPager or FragmentPagerAdapter buffers his state, even though I never wrote code for this purpose? I know, without my code here, this is very hard to answer, but the app is bigger (it's the officially university app of duesseldorf).

If someone need more information, please write/ask :)

Thx.

Tobias S
  • 1,275
  • 8
  • 23

1 Answers1

0

Luckily the answer is in Remove Fragment Page from ViewPager in Android :)

Problem is, that the FragmentManager itself saves old fragments, so we have to "manipulate" ID's of the fragments :)

Additionally we should clear the back stack, due to memory issues. Here is an example:

@Override
public void onPause() {
    clearBackStack();
    super.onPause();
}

private void clearBackStack() {
    FragmentManager manager = getFragmentManager();
    if (manager.getBackStackEntryCount() > 0) {
        FragmentManager.BackStackEntry first = Manager.getBackStackEntryAt(0);
        manager.popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
}
Community
  • 1
  • 1
Tobias S
  • 1,275
  • 8
  • 23