1

Problem: Activity B ALWAYS return false, however, if I check the same KEY inside activity A, the result is true. Why is this? I have checked the KEY, it is exactly the same in both activities.

Edit: Thank you all for your help, however after implementing your suggestions, I still have had no luck. Here is what I have now:

ActivityA, extends Activity Implements OnGesturePerformedListener

//How methods are called...
read(getApplicationContext(),"fav", posName);
write(getApplicationContext(), "fav", posName, false);
...

public Boolean read(Context context, String name, String key) {
    SharedPreferences sP = context.getSharedPreferences(name, MODE_PRIVATE);
    Boolean b = sP.getBoolean(key, false);
    return b;
}

public void write(Context context, String name, String key, boolean value) {
    SharedPreferences sP = context.getSharedPreferences(name, MODE_PRIVATE);
    SharedPreferences.Editor editor = sP.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

ActivityB, extends FragmentActivity, sub class:

public static class SectionFragment extends Fragment

Inside onActivityCreated()

SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
Boolean b = sP.getBoolean(posName, false);

Results = b always equals false

Any Ideas?

  • What value does the variable `posName` set? Will it always have the same value? You can use the DDMS view to inspect the value of your shared preferences, take a look http://stackoverflow.com/questions/14913026/edit-shared-preferences-from-ddms – amatellanes Jul 17 '13 at 16:51
  • Can you provide some more contextual code within the actual program? The code we see here seems okay, but it's hard to tell without seeing it in use. – Brian Jul 17 '13 at 17:35
  • Hi guys, I have fixed it. The error was a key mismatch. Sorry forgot to update this post. Thanks! –  Jul 17 '13 at 18:07

3 Answers3

2

Use getSharedPreferences`:

public void write(Context context, String key, boolean value) {

    SharedPreferences sharedPreferences = context.getSharedPreferences(
            NAME_SHAREDPREFERENCES, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

public Boolean read(Context context, String key) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(
            NAME_SHAREDPREFERENCES, MODE_PRIVATE);
    Boolean b = sharedPreferences.getBoolean(key, false);
    return b;
}
amatellanes
  • 3,645
  • 2
  • 17
  • 19
2

From the Android docs:

Activity persistent state is managed with the method getPreferences(int), allowing you to retrieve and modify a set of name/value pairs associated with the activity. To use preferences that are shared across multiple application components (activities, receivers, services, providers), you can use the underlying Context.getSharedPreferences() method to retrieve a preferences object stored under a specific name. (http://developer.android.com/reference/android/app/Activity.html)

So basically you need to use Context.getSharedPreferences() to share preferences across multiple activities.

Brian
  • 1,436
  • 8
  • 18
  • you can get the current context by using this method, getApplicationContext(); So, for accessing shared preferences in other activities just use getApplicationContext().getSharedPreferences(); – Salman Khakwani Jul 16 '13 at 18:09
0

You can adopt the following approach to get same sharedPreference everywhere:

SharedPreference mPrefs =  PreferenceManager.getDefaultSharedPreference(getApplicationContext());
 SharedPreference.Editor mEditor = mPrefs.edit();

I hope it was useful.

Abhishek Shukla
  • 1,242
  • 8
  • 11