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.

Shared preferences

Although, there are many ways to overcome a 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.

  1. Add the OnSharedPreferenceChangeListener and then override onSharedPreferenceChanged() method.
  2. Register and Unregister the Shared Preferences objects.
  3. 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.

Adding Shared Preference Change Listener


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 the 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, Click here check out this video tutorial.


References



Translate »