Current set up
| -> FragmentA
| -> FragmentB -+
+- Activity2 <-------+
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
I solved it by moving those fragments under Activity1a
So now it looks like this
+-> Activity1 --------+ | | | Activity1a <------+ | | -> FragmentA | | -> FragmentB | +-----------------------+ | | +-- Activity2 <------------+
Activity1a is now an invisible Activity that holds on to FragmentA and B. When I want to transition to Activity2, now I just need to
finish() from Activity1a, then all fragments attached to Activity1a will be gone.
From Android official document: https://developer.android.com/guide/components/fragments.html
A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments.
This solved the problem. I just needed to handle corner cases like how
onBackPressed() and such are used.