Open the app, set a deadline, and turn the phone sideways so it switches to landscape mode. What happened?? Just turning the phone sideways made the app forget the deadline! Clearly this must be a bug in Android!?

To understand what happens, we need to understand that mobile apps work differently from other apps. As Apple puts it:

“Your app can be interrupted at any time. When an interruption occurs, your app should save the current state quickly and precisely so people can seamlessly continue where they left off when they return. “

An “interruption” usually happens when the user doesn’t use your app for a while, or when there’s little battery left and a phone call comes in. Both iOS and Android has plenty of tools to make it as easy as possible to save and restore the current state.

You might have noticed already that our onCreate method has a parameter Bundle savedInstanceState. This bundle is used to restore our state. When it is time to save the state, Android will call onSaveInstanceState(Bundle outState). Whatever you put in outState will come back in savedInstanceState when the activity is recreated!

So, back to our original question: why does the app forget the deadline when we rotate the phone?? Well… the people who made Android in the first place figured that since every activity should be able to save and restore their state automatically anyway: When the phone is rotated, they decided to simply save the state, throw away the activity and create a new one! This turned out to be a super-easy way to handle practically all configuration changes, including the tricky ones:

  • resizing windows on Chromebooks
  • split screen on tablets
  • moving the activity to a TV screen (HDMI or Chromecast)
  • changing font, scaling factor
  • switching language

Next: Save the date! (Kotlin version)