Answer the question
In order to leave comments, you need to log in
How to get rid of java.lang.IllegalStateException when calling addToBackStack?
Calling the addToBackStack
method sometimes results in a java.lang.IllegalStateException: Activity has been destroyed . And how to get rid of it?
public void addMainFragment() {
hideKeyboard();
if (mainFragment == null) {
mainFragment = new MainFragment();
}
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.containerFrameLayout, mainFragment, "MainFragment");
ft.addToBackStack("MainFragment");
ft.commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
}
Answer the question
In order to leave comments, you need to log in
So everything seems to be obvious - addMainFragment () is called at the wrong time. Here you need to look at the code of the entire activity and analyze why addMainFragment() is called on a dead activity instance. Perhaps when the activity is recreated, some background task finishes its work and tries to do something with the instance of the activity, to which it is rigidly attached ... Most often, these kinds of problems arise.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question