onRetainNonConfigurationInstance and getLastNonConfigurationInstance

This is the Etch A Sketch app we saw here, but it no longer erases the drawing when we change the orientation. The methods onRetainNonConfigurationInstance and getLastNonConfigurationInstance of class Activity are deprecated since the introduction of Fragments in Android 3.0 (SDK 11), , but I still have to use them because my Android phone only has SDK version 8. See Retaining an Object During a Configuration Change.

We can use this machinery to save and restore a Path, because a Path does not contain a Context. We could not have used this machinery to save and restore a View.

To erase the drawing, you will now have to go to the Settings app and stop the Etch A Sketch app.
Settings → Applications → Manage applications → Etch → Force stop

Source code in Etch.zip

  1. EtchActivity.java: the EtchView is now a field.
  2. EtchView.java: the constructor now takes an extra argument.
  3. main.xml: ignored.
  4. AndroidManifest.xml: untouched.