Join Chiu-Ki Chan for an in-depth discussion in this video SharedPreferencesFavorites, part of Effective Android Testing for Mobile Developers.
- [Instructor] To start a favorites, we are going to use shared preferences. Go to the Java folder, right-click on data local, new Java class. Call it shared preferences favorites. Create a private final share preferences field. It is not initialized yet, so press Alt+Enter and say initialize in constructor.
Accept the default value of null for now. To initialize it, we will need a context, so we'll edit as a perimeter to the constructor in line eight, context context. Then, go to the next line and replace null with context.getSharedPreferences. We'll give it the file name favorites.xml. And then, we are going to call the mode context.MODE_PRIVATE, meaning that we do not want other apps to have access to it.
For favorites we will need three operations. Get, to retrieve what is previously stored, put, to stall a new value, and then we'll have a convenience method toggle, which retrieves the old value, flip it from true to false or false to true, and then put it inside the stall and then return the value so that we can display it. Let's do it one at a time. First, get public boolean get.
We'll check the ID, which is the recipe ID. Inside the function, return the shared preferences.getboolean, and then the key will be the ID. And then the next parameter is the default value, meaning that if we don't find it in the shared preferences what we should return. I will say false. Next, let's implement put, public void put. This should take two parameter.
One, the recipe ID, and then a boolean to indicate whether I want to save it as a favorite or not. Inside, we are going to call shared preferences.Editor, so that we can have the Editor from the preferences. Next, if it is a favorite, we are going to call Editor.putboolean with the ID and the value true because it's a favorite.
Otherwise, we're not going to put the value false. But instead, we are going to call Editor.remove with the ID because in cat, we are going to retrieve false when we don't find the ID. Afterwards, don't forget to call Editor to apply so that all these changes are actually saved. Finally, we are going to implement toggle public boolean toggle, which will take a string, the recipe ID.
Inside we will need to retrieve the value first before we negate it and then put it back into the database, boolean favorite equal get ID. After we retrieve it, want to put it back with the same ID but the negated value. So, put ID comma exclamation point favorite. Finally, we are going to return exclamation point favorite. In other words, the toggled value so that we can use it in the UI.
Now that we have a way to store the favorites, let's hook it up in recipe activity. Go to recipe activity. Right after the now check in line 33, we are going to create a shared preferences favorites. Share preferences favorites, and we'll call it favorites equal new shared preferences favorites this, meaning the context is activity. Next, we're going to retrieve from this favorites the value for our particular recipe, boolean favorite equal favorites.get.
We will need an ID, which is recipe.ID. Next, we will need to display this favorite value. Right after the set text for title in line 38, add titleview.setselected, and then passing the favorite as the boolean. Run the app to make sure everything looks good. Looks like it compiled. Click on chocolate pudding. And then, you can try to click on the star, but nothing happens yet because we have not implemented the on-click listener.
However, we are showing the favorite, which is not true by default and therefore the star is hollow. Next, let's implement the on-click listener so that when you click on the star, it's going to remember that you like chocolate pudding.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP