Empty savedInstanceState Bundle when restoring a fragment after double rotation when replacing a fragment

allows you to call them Fragment A and B. Fragment B is just a detailed view for A, which replaces fragment A with the click of a button in fragment A.

Replacement Code:

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, new DetailFragment());
    transaction.addToBackStack(null);
    transaction.commit();

When I now rotate the screen in fragment B once and click Back, the old fragment A is restored without any problems (it restores its state to onActivityCreated using the savedInstanceState package).

Now for the interesting part ...

When I rotate the screen in fragment B more than once and click Back, I get a NullPointerException because int[] data = savedInstanceState.getIntArray(STATE_DATA);in onActivityCreatedreturns null.

How can I fix this behavior? The only other way I was was through persistent storage (preference or DB), but this seems very inappropriate for use.

edit / additional information: the kit itself is not null, it is just empty

+4
source share
1 answer

Ok, I found the answer:

The following methods from fragment A are called when rotation changes with active fragment B: onSaveInstanceState (), onAttach (), and onCreate ()

onActivityCreated ( sdk!). , , , onSaveInstanceState. , .

. onCreate(), , onSaveInstanceState .

+3

All Articles