onSaveInstanceState

How to persist data across changes in state such as device rotation. The onSavedInstanceState function stores information in a Bundle and is limited in the data it can contain (small amounts of data which can be easily serialised and de-serialised). It does not persist on app closure. **Try to keep the contents to considerably less than 100KB as this resides in RAM**

This example overrides the onPause and onSaveInstanceState functions to append an update to the screen when these events occur. When the screen is rotated the onSaveInstanceState function is called – it takes the contents of the screen (in particular the tv_lifecycle_events_display TextView) and stores it in the savedInstanceState Bundle. When onCreate is called after screen rotation, it checks for the LIFECYCLE_CALLBACKS_TEXT_KEY in the Bundle and, if present, writes the data back to the screen.

Add the following to your Activity file:

private static final String LIFECYCLE_CALLBACKS_TEXT_KEY = "callbacks"; // Key string constant for the data you will store
private static final String ON_PAUSE = "onPause"; // The text that will be displayed onscreen when the onPause state occurs
private static final String ON_SAVE_INSTANCE_STATE = "onSaveInstanceState"; // The text that will be displayed onscreen when the onSaveInstanceState state occurs

    protected void onCreate(Bundle savedInstanceState) { // Standard onCreate override
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        private TextView mLifecycleDisplay = findViewById(R.id.tv_lifecycle_events_display); // TextView the data will be printed to


        /*
         * If savedInstanceState is not null, that means our Activity is not being started for the
         * first time. Even if the savedInstanceState is not null, it is smart to check if the
         * bundle contains the key we are looking for. In our case, the key we are looking for maps
         * to the contents of the TextView that displays our list of callbacks. If the bundle
         * contains that key, we set the contents of the TextView accordingly.
         */
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey(LIFECYCLE_CALLBACKS_TEXT_KEY)) { // If key is present...
                String allPreviousLifecycleCallbacks = savedInstanceState.getString(LIFECYCLE_CALLBACKS_TEXT_KEY);
                mLifecycleDisplay.setText(allPreviousLifecycleCallbacks); // ... display all of the previous contents to the TextView
            }
        }


    @Override
    protected void onPause() {
        super.onPause();
        mLifecycleDisplay.append(ON_PAUSE + "\n"); // Displays the event onscreen
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) { // Override onSaveInstanceState
        super.onSaveInstanceState(outState); // Call super class - ALWAYS DO THIS
        mLifecycleDisplay.append(ON_SAVE_INSTANCE_STATE + "\n"); // Displays the event onscreen
        String lifecycleDisplayTextViewContents = mLifecycleDisplay.getText().toString(); // Gets all of the content from the TextView
        outState.putString(LIFECYCLE_CALLBACKS_TEXT_KEY, lifecycleDisplayTextViewContents); // Puts TextView content into savedInstanceState Bundle
    }

}