In this article, we will learn about Shared Preference On Change Listener interface.
We know Android App development is a big task but, you know there is much fun associated with it. So basically I ran into a situation where I had some key-value pairs stored in Shared Preferences. But whenever I change those values in there, I would not see any changes reflected in my Activity classes or Fragment classes.
Although, there are many ways to overcome the situation like that if we are using Shared Preferences. We could override the default onBackPressed() method or override the onResume() method in our activity, then set the value from Shared Preferences again.
But I was looking for something much simpler by which I can update the UI immediately. And I found the uses of Shared Preference On Change Listener which is super easy to implement.
Implement of the Shared Preference Change Listener Interface
There are three simple steps to implement this interface.
- Add the OnSharedPreferenceChangeListener and then override onSharedPreferenceChanged() method.
- Register and Unregister the Shared Preferences objects.
- Update the UI according to the changes.
So how should we add OnSharedPreferenceChangedListener interface?
First we have to understand where we want our App’s UI needs to be updated. So it could be an Activity or a Fragment class. And beside that class name we must use implements then the name of the interface SharedPreferences.OnSharedPreferenceChangeListener as shown in the below code snippet. After that we have to override onSharedPreferenceChanged() method.

Registering And Unregistering Shared Preferences Objects
Now we have to create two override methods inside our Activity class onStart and onStop method. In order to registering and unregistering a Shared Preferences object. We can also use onCreate and onDestroy method for that.
public class HomeActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { } @Override protected void onStart() { super.onStart(); SharedPreferences pref = getSharedPreferences("MY_PREF_FILE", Context.MODE_PRIVATE); pref.registerOnSharedPreferenceChangeListener(this); } @Override protected void onStop() { super.onStop(); SharedPreferences pref = getSharedPreferences("MY_PREF_FILE", Context.MODE_PRIVATE); pref.unregisterOnSharedPreferenceChangeListener(this); } }
Anyway, now we need to create a Shared Preference object. Then get the preference instance and call registerOnSharedPreferenceChangeListener() inside onStart.
For unregistering that preference object, again we have to create the same object or instance of that preference file. Then inside onStop method we can call unregisterOnSharedPreferenceChangeListener(). As shown in the above code snippets.
Updating UI Data using Shared Preference Change Listener
Here in this onSharedPreferenceChanged method, we have a Shared Preferences object and a String variable called a key. Therefore we can get all the keys from shared preferences and check whether a particular key’s value is changed or not. As shown in below code snippets.
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("high_score")) { //Update High score txtHighScore.setText("Highscore: " + sharedPreferences.getInt("high_score", 0); } else if (key.equals("theme_change_key")) { //Update UI theme color } }
Remember: We must register and unregister the Shared Preferences objects. Otherwise, the interface will not be able to work any more.
Conclusion:
Almost every day in any android applications developers are using this Shared Preference On Change Listener. Because as of now we know how easy it is to implement in our projects. If you want to get some practical example, check out this video tutorial.
References
1 Comment
Android Shared Preferences Fundamental | Daily Coding · December 5, 2020 at 12:40 pm
[…] we will be discussing on another important topic OnSharedPrefernceChangeListener. So that’s all for today, catch you in the next […]