0

I saved a SharedPreferences value in one class now i want to access the value form different java class.I have used below code to save value

public  void Savetz(String value){
     SharedPreferences  sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
       SharedPreferences.Editor editor = sharedPreferences.edit();
       editor.putString(TIMEZONE, value);
       editor.commit();
}

 public  String Loadtz(){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String tz=sharedPreferences.getString(TIMEZONE, "");
        return tz;

    }

I have tried by calling Loadtz() from another class by object but it shows "unfortunately app stopped working message ".how can i do this .please give any clue

3 Answers3

0

use getSharedPreferences(String name, int mode) instead of PreferenceManager.getDefaultSharedPreferences(Context contex)

Vigen
  • 483
  • 6
  • 12
  • please explain if it it is not difficult – Vigen Dec 31 '14 at 16:07
  • Yup both are same in most generic ways. This can never be the reason of crash. @Vigen: http://stackoverflow.com/questions/5946135/difference-between-getdefaultsharedpreferences-and-getsharedpreferences – Rohit5k2 Dec 31 '14 at 16:13
0

Change your both method slightly by adding in a Context parameter. In the following code, I made both method to be static so they can be access anywhere as long as you have a valid Context (Activity, Service, or your Application class) to feed into the method param:

public static void Savetz(Context context, String value) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    sharedPreferences.edit()
                     .putString(TIMEZONE, value)
                     .commit();
}

public static String Loadtz(Context context) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String tz = sharedPreferences.getString(TIMEZONE, "");
    return tz;
}
petey
  • 16,914
  • 6
  • 65
  • 97
-1

I think you can access from any class. You just need to use the same tag.
Try using this:

Class A:

private static final String TIMEZONE = "timezone";
//-----------
    public  void Savetz(String value){
           SharedPreferences sharedPreferences= this.getPreferences(Context.MODE_PRIVATE);
           SharedPreferences.Editor editor = sharedPreferences.edit();
           editor.putString(TIMEZONE, value);
           editor.commit();
    }

Class B:

 private static final String TIMEZONE = "timezone";
    //-----------
     public  String Loadtz(){
          SharedPreferences sharedPreferences= this.getPreferences(Context.MODE_PRIVATE);
          String tz=sharedPreferences.getString(TIMEZONE, "");
          return tz;
        }
rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56
  • That wouldn't crash the app! – Rohit5k2 Dec 31 '14 at 16:05
  • and the point is don't crash the app right? I didn't understand your comment – rafaelasguerra Dec 31 '14 at 16:30
  • It means that app is crashing in the question and the change you suggested wouldn't stop it from crashing. Because both methods are in same class and in both methods same key variable is being use, so in reality you are not suggesting any real change. – Rohit5k2 Dec 31 '14 at 16:32
  • Maybe is crashing because he needs to instantiate the SharedPreferences object before onCreate. We need more information about the problem. – rafaelasguerra Dec 31 '14 at 16:38
  • Exactly! We don't know the cause. And your answer is totally unrelated. – Rohit5k2 Dec 31 '14 at 16:41