0

I have this code:

SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("allphones",allphones);
editor.putString("specialkey", someKey);
editor.commit();

The allphones parameter gets stored, but when I try to get the "specialkey" in the next Activity like this:

String allphones = getPreferences(Context.MODE_PRIVATE).getString("specialkey","Blabla");

It ALWAYS returns me the Default String "Blabla".

  • are you sure someKey is not null when you put the preference ? – SebastienRieu Nov 15 '19 at 14:50
  • @SebastienRieu 100% sure – Markus Haas Nov 15 '19 at 14:52
  • 1
    `getPreferences()` returns a `SharedPreferences` that is specific to a given `Activity`, so your second `Activity` is reading from a completely different file than the one that the first `Activity` saved to. Use `getSharedPreferences()` instead, where you can specify the filename, or possibly `PreferenceManager.getDefaultSharedPreferences()`, which uses a single default filename throughout your app. – Mike M. Nov 15 '19 at 14:52
  • @MikeM. ^^ should be an answer and not a comment! – Alex Nov 15 '19 at 15:25
  • @Alex I've already closed it as a duplicate. – Mike M. Nov 15 '19 at 15:26
  • 1
    @MikeM. Need my coffee LOL – Alex Nov 15 '19 at 15:26

0 Answers0