Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Exploring local data storage options
- Creating an Android virtual device
- Starting a new project
- Defining preferences with Java and activities
- Creating and reading JSON and XML data files
- Creating a new SQLite database
- Inserting and retrieving data in the database
Skill Level Intermediate
There are many scenarios in an Android application where you need to instantly react when a preference is changed and update the user interface in some way. When you need to this, you can create a preference listener, an event listener that will let you know when a preference has been updated and let you add code that executes when that happens. I'll work in a new version of the project called PreferenceListener. Just as in the last video, this version of the project has buttons to set and show preferences, but I'll update this version of the application so that the preferences are updated instantly when they're changed at runtime.
I'll go to the application's MainActivity Java class, and I'll start by creating an instance of a listener object. I'll declare the listener object as a field of the class, so that it persists as long as the activity is on the screen. You'll see that developers sometimes create this listener objects within an onCreate Method. The problem with that, though, is that they're weakly referenced and subject to garbage collection, and so they don't always work correctly. By declaring the listener outside of the methods, you make sure it lasts as long as you need it to.
Within the activity, I'll declare a new private field, and I'll set the datatype as OnsharedPreferenceChangeListener. That's a long class name, so I'll just type the beginning of the class and press Ctrl+Space and select the class, and I'll name the object Listener. Now, you can't initialize it here; you have to wait until the activity is coming to the screen. So, I'll add code to the OnCreate method. I'll move the cursor after the code that's initializing the settings object, and then I'll initialize it using this code.
I'll say Listener = new, and then I'll use the class name again and call its Constructor method. I'll type in the name of the class. I'll press Ctrl+Space and I'll use the version that's a member of SharedPreferences: SharedPreferences .OnSharedPreferenceChangeListener. I'll select it and that adds the method's signature and also adds the required method OnSharedPreferenceChanged. You add the code that you want to execute within that new method, where the TODO comment is.
When this method is called, you'll receive a reference to the SharedPreferences object and the key to the preference that was changed. You can write finely tuned code that only reacts to the one preference that was changed, or, as I'm going to do here, you can call a method in your main activity that simply updates everything that the preferences might affect. I will get rid of this TODO comment, and within the class, I'll reach back to the current instance of the main activity, using MainActivity.this.
And from there, I'll call the method refreshDisplay. The refreshDisplay method is designed as an event handler method. It receives a view argument. I'm going to be calling it without an event, so I don't need to pass in a view reference. I'll just pass in null. And that will cause the display to update, taking into account any preferences that might have changed. Finally, I need to register the Listener object with the Preferences object. That's how you bind the two objects together.
When the user makes a change to the preferences, the Preferences object looks for listeners, and the only Listeners that have registered with the Preferences object will receive the event. After the listener has been created, I'll call settings. registerOnSharedPreferenceChangeListener. Once again, I'm using autocomplete to call this very long method name. And I'll pass in the Listener object. Now, the refreshDisplay method is already doing what I need it to do.
It's down here at the bottom of the class, and it's updating the display based on both the USERNAME and the VIEWIMAGES Preferences. So I'll run the application in the emulator. Notice that as it opens, the preferences aren't displayed automatically. I'll click on Show preferences and that still works as it did before. But now I'll click on Set preferences. That take me to my Preference Activity. I'll click on the User name, and I'll update it by adding the first initial of my last name, and then I'll deselect the View images preference.
Then I'll click the Back button. And when I come back, the screen has been already updated. I don't need to click to Show preferences button. And in fact, I'll go back to the application in Eclipse and I'll take out that button. I'll go to the MainActivity XML file, and I'll click on the Show preferences button, and then I'll just press Delete and it's gone. I'll save the changes and run the application again. And once again, I'll go into Set preferences.
I'll change the User name. This time I'll add my full name, and I'll select the View images preference to turn it to true. And I'll return, and the application has been updated. So, an event listener will let you react instantly whenever preferences are changed. You can use the listener on either a SharedPreferences object that's managed by an activity, as I'm doing here, or on Preference objects that you've created in Java, using the getPreferences and getSharedPreferences method.
Either way, you'll be able to update your user interface right away, whenever the user makes a change.