Saving Data Across Rotation

Android does a great job of providing alternative resources at the right time. However, destroying and recreating activities on rotation can cause headaches, too, like GeoQuiz’s bug of reverting back to the first question when the device is rotated.

To fix this bug, the post-rotation QuizActivity needs to know the old value of mCurrentIndex. You need a way to save this data across a runtime configuration change, like rotation. One way to do this is to override the Activity method

 ​ ​ ​ ​p​r​o​t​e​c​t​e​d​ ​v​o​i​d​ ​o​n​S​a​v​e​I​n​s​t​a​n​c​e​S​t​a​t​e​(​B​u​n​d​l​e​ ​o​u​t​S​t​a​t​e​)​

This method is normally called by the system before onPause(), onStop(), and onDestroy().

The default implementation of onSaveInstanceState(…) ...

Get Android Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.