I've observed very strange Android fragment allocation behaviour in my application recenty.
My application has two fragments and two layouts:
- portrait layout with ViewPager as root view;
- landscape layout without Viewpager (in this example LinearLayout is used as root view).
The reason for this setup - I can display all fragments simultaneously while in landscape mode.
And this works fine until we don't look inside heap more closely. I've looked and I saw that when:
- I start application in portrait mode with ViewPager;
- change orientation to landscape, expecting that two new fragments will be created;
- four fragments are created, all in RESUMED state.
So I've played with it for a while, tried different setups, google it etc and find no answer. Finally, I've create demo application by Android Studio wizard (ViewPager with tabs), remove some boilerplate, add landscape layout to "layout-land" folder and push it to GitHub.
I do not quote sources here for the brevity sake, hope it's okay.
My questions are:
- Is this pattern I've selected is inappropriate? Maybe it's better to use ViewPager in both layouts but show 2 pages simultaneously in landscape mode like here? Or totally get rid of ViewPager and use more classic Lars Vogel's multi-pane pattern?
- Can it be a bug in Android runtime?
- And if it's a proper pattern and not an Android runtime bug, why these dublicate fragments are being allocated, for Christ's sake?