I
I
Igor2014-09-07 21:39:27
Java
Igor, 2014-09-07 21:39:27

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

2 answer(s)
A
Alexander, 2014-09-25
@DrZ0idberg

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.

N
naXa!, 2015-02-18
@naXa

Find the root of the problem and get rid of it. An IllegalStateException indicates that a method was called at the wrong time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question