I am working on an android app which has 2 activities, first being MainActivity and Second is UpdateActivity. In my main activity, I have fragments and In these fragments, I am nesting one more layer of the fragment using childFragmentManager using the code given below,
fun changeFragmentTransfer(int: Int,index : Int? = null){
fragment = when(int) {
0 -> DemoLocalFragment.newInstance(10, "")
1 -> ConfigureLocalFragment.newInstance("","")
2 -> LocalListingFragment.newInstance("","")
else -> DetailLocalFragment.newInstance(index!!,"")
}
if (index == null)
childFragmentManager
.beginTransaction()
.replace(R.id.baseLocalFrameLayout,fragment).commit()
else {
(activity as MainActivity).replaceFrameInMainActivity(fragment!!)
}
}
Everything works just fine if I open UpdateActivity after this function is executed But if I start UpdateActivity from MainActivity before execution reaches this point then my app crashes.
I don't understand why this code runs in the first place when the MainActivity is in the back-stack.
I checked these questions related to the same topic link1 link2 link3 link4, but here they suggest to use commitAllowingStateLoss() instead of commit() which I can't afford as my fragments depend on this saved instance state. So is there an alternate solution in which I can preserve saved instance states.
The errors that I am getting is as follow.
Process: com.idea_implement_india.bloombench_and_kossine.iotcontrollerapp, PID: 21132 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053) at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079) at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678) at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632) at com.idea_implement_india.bloombench_and_kossine.iotcontrollerapp.Fragments.BaseLocalFragment.changeFragmentTransfer(BaseLocalFragment.kt:94) at com.idea_implement_india.bloombench_and_kossine.iotcontrollerapp.Fragments.BaseLocalFragment.changeFragmentTransfer$default(BaseLocalFragment.kt:84) at com.idea_implement_india.bloombench_and_kossine.iotcontrollerapp.Fragments.DemoLocalFragment$subHandler$1.handleMessage(DemoLocalFragment.kt:141) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:238) at android.app.ActivityThread.main(ActivityThread.java:6016) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
Here in this error line
com.idea_implement_india.bloombench_and_kossine.iotcontrollerapp.Fragments.BaseLocalFragment.changeFragmentTransfer(BaseLocalFragment.kt:94)
corresponds to this line in the code
childFragmentManager
.beginTransaction()
.replace(R.id.baseLocalFrameLayout,fragment).commit()
If someone knows of any alternate solution to this problem then please do tell. Thank you in advance.