Android savedInstanceState is always null

I have the following situation:

  • Activity A starts activity B
  • Action A calls methods onPause, onSaveInstanceStateand onStop.
  • In step B, I press the UP button on the action bar
  • Action A is first destroyed (called onDestroy) and then recreated. (This seems to be happening this way. I haven't implemented this path, it just seems like it's an Android way.)
  • During the method, the onCreatevariable is savedInstanceStatealways zero.

I know that there were similar topics, but none of them seemed to respond to my situation. All callback methods have log lines in them, so I’m sure that the save method is executed and destory is running. But why is there no object savedInstanceState?

My save method:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putSerializable(OpenSessionsActivity.SESSION, session);
    System.out.println("saving ..");
    super.onSaveInstanceState(savedInstanceState);
}

- , ?

+4
4

Up , , , back.

, , Activity B:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
+7

Up. A,

A) A "singleTop" ( android:launchMode="singleTop" AndroidManifed.xml)

B) FLAG_ACTIVITY_CLEAR_TOP up B:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent up = NavUtils.getParentActivityIntent(this);
            up.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            NavUtils.navigateUpTo(this, up);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

:

, , . singleTop ( FLAG_ACTIVITY_CLEAR_TOP), , . NewIntent(). ( FLAG_ACTIVITY_CLEAR_TOP), , .

+8

super.onSaveInstanceState(savedInstanceState);

.

0

:

void onSaveInstanceState (Bundle outState):

, onPause(), , , onStop(), . , onPause() onStop(), , B A: onSaveInstanceState (Bundle) B, , . , onPause(), onSaveInstanceState (Bundle), B A: the system can avoid calling onSaveInstanceState (Bundle) in step A if it has not been killed during life from B, as the state of user interface A remains unchanged.

If you change the orientation using your device onStop () and onSavedInstanceState (...) is called.

0
source

Source: https://habr.com/ru/post/1532120/


All Articles