-1

Hi all I am using shared preferences in my application. I am using action bar. In this action i am having customized spinner. I have store the value into the shared preference while onNavigationItemSelected in my main activity. Which means tab zero. Lie this.

      main_prefs = getSharedPreferences("final_city_id", MODE_PRIVATE);
               // detail is sharedpreference name
             SharedPreferences.Editor editor = main_prefs.edit();

            // to remove paricular value from detail 

            String spusername = main_prefs.getString("changed_city_id", "novalue");

            if(spusername!= null && !spusername.equals(""))
            {   
                System.out.println("inside if condition........");
                Log.e("Spusername != null","-->"+spusername);
                editor.remove("changed_city_id"); 
                Log.e("value removed","-->"+spusername);
                editor.putString("changed_city_id",spinner_city_id);
                editor.commit();
                String spusername1 = main_prefs.getString("changed_city_id", "novalue");

               System.out.println("spusername1```````````"+spusername1);

            }
           else
           {
               System.out.println("inside else condition........");
               editor.putString("changed_city_id",spinner_city_id);
               editor.commit();
               String spusername2 = main_prefs.getString("changed_city_id", "novalue");

               System.out.println("spusername2```````````"+spusername2);
           }

Now i move on to tab 2 or 3 and then again selecting spinner. My values has to be change. And my shared preference value is also change. Now I want to check if the shared preference is changed or not in my 2 tab and my third tab. Which means another activity. But my spinner's onNavigationItemSelected methos is in my zero tab. So i am using OnSharedPreferenceChangeListener. In some link suggest this one. Because when shared preference is change this override method is automatically triggered. I searched lot of things and tired it. but i can't able get in clear way. Can anybody clear me? Thanks in advance.

malavika
  • 1,331
  • 4
  • 21
  • 54

2 Answers2

1

Try out as below:

   public class myClass implements OnSharedPreferenceChangeListener
 {
  private SharedPreferences settings;

settings = PreferenceManager.getDefaultSharedPreferences(this);
settings.registerOnSharedPreferenceChangeListener(this);

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
   // Process it here
}
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
-1
prefs.registerOnSharedPreferenceChangeListener(
  new SharedPreferences.OnSharedPreferenceChangeListener() {
  public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    // Implementation
  }
});

instead of this

// Use instance field for listener
// It will not be gc'd as long as this instance is kept referenced
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
  public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    // Implementation
  }
};
prefs.registerOnSharedPreferenceChangeListener(listener);
dipali
  • 10,966
  • 5
  • 25
  • 51