I have a parent Fragment Activity that has a ViewPager which contains a child ViewPager. The child ViewPager contains Fragments for each page. I communicate between these child page fragments and the top parent Fragment Activity using a callback interface e.g.
public interface Callbacks {
public void onItemSelected(Link link);
}
In the parent Fragment Activity I listen for onItemSelected
events e.g.
@Override
public void onItemSelected(Link link) {
Bundle argumentsFront = new Bundle();
argumentsFront.putParcelable(FragmentComments.ARG_ITEM_ID, link);
fragmentComments = new FragmentComments();
fragmentComments.setArguments(argumentsFront);
getSupportFragmentManager().beginTransaction().replace(R.id.post_container, fragmentComments).commitAllowingStateLoss();
}
Now this works fine when the app is first launched.
If you turn the device to change the orientation the Activity restarts. All fragments reinitialise themselves as I use setRetainInstance(true);
(I do not call setRetainInstance(true) in the page Fragments of the child ViewPager as it is not supported). However if I click a list item in the Fragment of the child ViewPager I get this exception:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
Does anyone know why this happens?
Thanks
I had this problem with nested fragments and none of the stackoverflow solutions worked for me. Just it seems, that there is a bug with support library, when dismissed fragments still store pointers to previous activity (so getFragmentManager() just returns null, because it is called on already destroyed activity), that's why you need to manage pointers yourself. I ended up with a following solution:
1. In the first level fragment I was saving pointer to the activity in the method
2. In the activity which handles fragments I ended up with this code:
You should pass parentActivity of FIRST level fragment only when you are calling SECOND level (nested) fragments, as it seems that this bug is only with nested ones after you bring your app from foreground.
Had a similar issue. Basically if the ViewPager just has couple of fragments, then store references to them in current activity. DO NOT call pagerAdapter's getItem() because it creates a new fragment and it is not attached to any activity and that's why we see "Activity has been destroyed" exception. If you don't want to keep fragment references, then you can use findViewWithTag() method to get Fragment object.
Committing transactions in OnPostResume callback fixed the issue for me. Thanks to following blogpost http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html
I had a similar issue, I think it is because the fragments are retained and are keeping a reference to a destoryed activity, my solution was to keep a reference to the fragment in the activity e.g Fragment myfragment = null. And then use the following code in MyFragment:
When you rotate the device, Android saves, destroys, and recreates your
Activity
and itsViewPager
ofFragments
. Since theViewPager
uses theFragmentManager
of yourActivity
, it saves and reuses thoseFragments
for you (and does not create new ones), so they will hold the old references to your (now destroyed) originalActivity
, and you get thatIllegalStateException
.In your child
Fragments
, try something like this:Then when a selection occurs:
Since
onAttach
gets called as part of theFragment
lifecycle, yourFragments
will update their callback reference on rotation.