Reputation: 744
I have 2 activities, A and B. Activity A consists of a fragment. For the notification click, I'm using TaskStackBuilder, calling nextIntent as B which has parentActivity as A. For the nextIntent, I'm setting Intent.FLAG_ACTIVITY_NEW_TASK flag.
The scenario is, B activity is in foreground, notification arrives and you click on it, Activity A gets destroyed, Activity B loads from onCreate. If I go back, Activity A's onCreate gets loaded, but for fragment commit call, I'm getting the following crash.
Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
Here's how I'm committing the fragment
EgFragment fragment = new EgFragment();
getSupportFragmentManager().beginTransaction().add(R.id.view, fragment).commit();
Any help to overcome this scenario.....
Upvotes: 1
Views: 836
Reputation: 744
I found out the problem. In FragmentManager, there are two booleans, mStateSaved and mDestroyed. In onStop, mSavedState will be set to true. In OnDestroy, mDestroyed will be set to true, but mStateSaved is not set to false.
Now, when Activity B is launched from Activity A, A's onStop sets mStateSaved to true. In B, when notification click occurs with FLAG_ACTIVITY_NEW_TASK. Both activities onDestroy will be called. Now, the onCreate gets called which makes the mStateSaved as false but after this, if we use getSupportFragmentManager and commit the fragment, mStateSaved will be true.
I think getSupportFragmentManager() is returning previous instance's manager where mStateSaved was true.
Now i do not know how to solve this.
Upvotes: 1