I am having a very frustrating issue with nested fragments. I have a seemingly simple and straight forward scenario:
MainActivity
ContentFrame
MainFragment
ViewPager (with FragmentStatePagerAdapter)
FragmentA
FragmentB
FragmentC
...
Note: I am not holding references to the fragments from outside of the pager adapter. I instantiate the fragments in the getItem method for the pager adapter.
Case 1
The issue I am experiencing is that the view pager fragments don't receive any state change. When I swap fragment in MainActivity, onPause and onResume are invoked on the MainFragment. However, onPause and onResume are not invoked on the view pager fragments.
When I switch back to MainFragment, onCreateView is invoked, creating a new ViewPager adapter, but as expected the nested fragments are not re-instansiated since they were tagged and can be reused. However, since onPause and onResume never get called, my state gets messed up.
Case 2
Furthermore, the ViewPager fragments never seem to get destroyed. If I instead explicitly remove MainFragment whenever I swap to a different outer fragment using:
fm.beginTransaction().remove(mainFragment).commit();
and later re-instantiating MainFragment. Even so, the ViewPager fragments never seem to get cleaned up. Now, the MainFragment instead ends up instantiating new ViewPager fragments and I eventually end up with many many ViewPager fragments that never get destroyed.
The Bottom Line
Essentially I just want to be able to acquire and release resources in the ViewPager fragments whenever MainFragment becomes active/inactive. Surely this can't be as difficult as it seems to be at the moment...