Saving data with SharedPreferences — Android #9

awesome icons from icons8.com as always

Here’s a short post on how to save small pieces of information in your app so you can use it again later. So you may want to save someone’s name, or a top score or anything else really that you want the app to “remember”.

There’s lots of different ways to save data in apps, today we’ll use the simplest — SharedPreferences.

To create an instance:

To set it up for use:

The “prefID” is important — this is how you identify it, you can change this to anything you want, but remember it as you’ll be re-using it.

To store something in the SharedPreferences:

and then:

The “nameKey” is the key — this is the identifier for the item of data you’re storing. You can call this anything, but when you’re retrieving the item, you need to use the same key. The “Bruce the Hoon” is the value — what is actually being stored.

Finally:

to actually “save” the info. Note you can also use:

but apply runs in the background so is theoretically better for app performance.

To retrieve the data:

The “getString” method here has two parameters — the first is the key you defined earlier and the second is a value that will be returned if there isn’t a value for that key.

Let’s try it out. Create a new project with an Empty Activity. In the activity_main.xml file, add the following:

  • a vertical LinearLayout and inside that:
  • a Plain Text EditText — give it an ID of “nameID”
  • a Number EditText — give it an ID of “numberID”
  • a Button — give it an onClick of “onButtonClick” and change the text to “Save Name and Age”
  • a TextView — give it an ID of “labelID”

Remember — we use the IDs to reference the objects in the matching Java file (here MainActivity.java).

The Component Tree should like this:

and the layout looks like this:

and for info, the XML looks like this:

So — the idea here is that the user will enter in a name and an age, we’ll save it to SharedPreferences when they hit the button — and then the next time the app is ran, it will check in SharePreferences for name and age values. If they are stored, we’ll display them in the TextView, if not we’ll show the default values.

Makes sense?

Either way, head to the MainActivity.java file. Once open, create a new variable for the SharedPreferences:

Create a new function onButtonClick:

The first lines get references to the TextView and EditTexts on our layout file:

These get the SharedPreferences ready:

and these store the values, taken from the EditTexts:

Note the “Integer.parseInt” — this is a handy method to turn a String into a int (a number).

and then we save:

and then change the TextView on screen to tell the user that we’ve saved the data:

Ok, now in the onCreate method, which is ran everytime the Activity is created on screen, add this:

The

gets the SharedPreferences.

gets the values stored. If there hasn’t been any values stored yet, “No name” and 0 will be returned for the name and age respectively.

gets a reference to the TextView in the layout and then show the stored name and age:

Ok, run the app:

We can see that the TextView shows “No name 0” — exactly as it should, as there’s nothing stored in the SharedPreferences yet!

Ok, fill in a name and age and hit the button, you should see the TextView change to Saved:

Now run the app again and you should see:

Awesome! We’ve pulled back the name and age from SharedPreferences and displayed them on screen!

Any issues or comments let me know below, and if you found it useful and want to shout it to the world, please hit the little heart button below! Thanks, Andy

Creator of Boxapopa, the iOS game for young kids with zero ads, just fun! https://apps.apple.com/ie/app/boxapopa/id1550536188