udacity / andfun-kotlin-dessert-pusher

Other
68 stars 276 forks source link

savedInstanceState always remains null after restart #10

Open houmie opened 4 years ago

houmie commented 4 years ago

savedInstanceState will always remain null after the app is killed. It is not passed on to the bundle.

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) { ... }
}

This bug exists since 5 years and Google has decided not to fix this anymore in 2020:

https://issuetracker.google.com/issues/37020082

As also discussed here on StackOverflow: https://stackoverflow.com/questions/6554317/savedinstancestate-is-always-null

I'm learning Android right now and find that quite a strange decision from Google. I hope there are other ways to persist the data and retrieve it at app start.

alfabravo2013 commented 3 years ago

Had this bug too, but it disappeared when I launched the app directly on the emulator instead of running it from Android Studio.

hirokihokari commented 3 years ago

I've found an interesting article. In it, two methods to "simulate when the system kills the activity due to memory shortage" are explained, both of which involve the developer option of the device.

One of them I tried out is to set Apps->"Don't keep activities" option on, which kills the activity when it goes background. The savedInstanceState worked as instructed.

I could deduct from this and Google's questionable response to the issue that it's obsolete, Android seemed to have change the way it kills the process, in that it doesn't technically kill or something, and if you do manually signal kill to the app now, the savedInstanceState won't behave as expected since that's not Android system does.

TheCodingArcher commented 3 years ago

@houmie - Test you app this way:

  1. Install the app in your emulator or mobile device
  2. Open AndroidStudio and connect your emulator or device.
  3. Check if it is showing in your Logcat
  4. Open app and start using the app (make some desserts)
  5. Put the app in the background
  6. Open terminal and hit the same command
  7. Reopen your app again

It will restart from the same saved state.

@2013alfabravo - thanks for the step 1 hint :)

SudKul commented 2 years ago

We are intentionally leaving this thread open. Also, we encourage new learners to use the https://knowledge.udacity.com/ platform to discuss specific issues with our experienced mentors.