4

If I do not explicitly call onSaveInstanceState(), which are possibilities, when savedInstanceState is not null in onCreate?

I see one option, when system recreates my app after crash - then savedInstanceState is not null.

Which are other options?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48
  • 3
    We do not call `onSaveInstanceState()` ever. It's a lifecycle callback and so Android calls it itself. – Sufian Jul 26 '17 at 08:30

1 Answers1

7

Which are possibilities, when savedInstanceState is not null in onCreate()?

  1. System was on shortage of memory, so it killed the process of your app. User navigates to your app, now onCreate() will be called with non-null Bundle.
  2. After configuration change (e.g. orientation change) - onCreate() will be called with non-null Bundle.

If I do not explicitly call onSaveInstanceState()

You shouldn't do that on your own. It's the system that will call activity lifecycle methods and onSavedInstanceState/onRestoreInstanceState.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • 1
    When I run app from Android Studio, if same activity is re-created on startup, then onSavedInstanceState() is also non-null. This happens even with instant run disabled. It's really annoying how onDestroy works differently when developing – Lou Morda Jun 12 '18 at 13:25
  • @LouMorda do you mean "savedInstanceState" paramter of the `onCreate()` instead, because a method being null/non-null makes no sense. – Sufian Dec 06 '18 at 10:10
  • @Sufian, did I mention *"null/non-null method"* in the answer? – azizbekian Dec 06 '18 at 10:24
  • @azizbekian I was responding to LouMorda's comment, hence it started with "@LouMorda". – Sufian Dec 06 '18 at 10:30