Persisting data - part 2

Last updated on 30 August 2024

We have implemented some code to save the user-selected colors of the the app to shared preferences, but you will notice that the app seems not to preserve the currently set colors and text because each time you run the app, it has the default black and white colors, along with an empty text field. We will fix that is this part of the project.

Looking at the setText instance method of MainActivity, we can see that the text input is saved to shared preferences. That is one piece of information we are already saving. We should also save the foreground and background colors to be used in the QR code. We do that by saving the foreground and background colors in setForeColor and setBackColor respectively, to the shared preferences using the preferenceHelper:

Now the app is saving all the important information the user might set: input text, foreground color and also background color of the resulting QR code. Let’s make sure the information is always loaded when the app is opened. We will take advantage the onResume method of an activity. You can read more about it along with other activity-lifecycle concepts here  External link . Create a new method named restoreSavedData and it will get the information saved in shared preferences and apply it to the appropriate widgets on the screen. Remember to call the restoreSavedData method from onResume.

Let’s do a test to see if the user’s color selections and input text are restored correctly when the app is reopened. First, run the app and set the colors along with some input text, and then close the app. Reopen the app and you will notice that while the input text is being restored, the colors are not – the default black and white colors are always shown when the app is loaded. The solution for this is simple if we start by looking at the onCreate method of MainActivity.

You will see that onCreate is calling setColorButtons, so let’s follow the code:

setColorButtons in turn calls the setForeColor and setBackColor methods. If you remember from above, the methods for setting the back and fore colors will immediately save the color values to the shared preferences. If you still remember, when the app is opened, the values for the fore and back colors are the default values – black and white. You can see these default values are set as the initial values of foreColor and backColor properties of the MainActivity class:

Now, to bring it all together in summary, when the app is opened, onCreate is always called and the when the color buttons have their values set, the default colors are saved in shared preferences. This means any previously saved values are overwritten. This is why the black and white default colors are always shown each time the app is reopened and the saved values are not displayed as we would have expected.

To fix this is simple: we have to load the saved colors first, before onCreate overwrites the saved values. Do this by adding a call to restoreSavedData before setColorButtons:

Does this mean we can ignore or remove the call to restoreSavedData in onResume? No – we still need that call. If our app is temporarily put in the background or is interrupted by by another app, we will still want the data to be restored when the app comes back to the foreground. onResume will handle that for us. Now if you run the app, the text input and colors are restored as we would expect. If you are still having issues with your code, you can compare it with the code we have here  External link .