Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharedPreferences return null value

//In one Fragment

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());

SharedPreferences.Editor editor = preferences.edit();
editor.putString("aKeyString", "aValueString");
editor.apply()

//If I try to log the just saved value the log is empty. Though I thought the apply(); committed that value to memory instantly and later to disc. So should be readable ?

Log.d(TAG, preferences.getString("aKeyString",""));
//nothing is logged to logcat at all. Not even a blank line. 

However it's in another Fragment that I need to read the value but is returning null.

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
Log.d(TAG, "value: " + preferences.getString("aKeyString",""));
//Logs out "value: null"

Why is it null? I'm using getDefaultSharedPreferences which would ensure I was accessing the correct data and getActivity().getApplicationContext() for same reason.

The posts on similar issues on SO regarding preferences returning null are suggesting to use :

'apply();' instead of 'commit();' and I already am. Or they suggest to use 'getDefaultSharedPreferences' instead of 'getSharedPreferences' and again I am.

Why is it null?

like image 373
RyanTCB Avatar asked Jan 06 '23 12:01

RyanTCB


2 Answers

You are doing it the right way, btw something strange is that you say it displays "null" while you specidfied a default empty string value, which should be impossible according to the javadoc :

/**
     * Retrieve a String value from the preferences.
     * 
     * @param key The name of the preference to retrieve.
     * @param defValue Value to return if this preference does not exist.
     * 
     * @return Returns the preference value if it exists, or defValue.  Throws
     * ClassCastException if there is a preference with this name that is not
     * a String.
     * 
     * @throws ClassCastException
     */
    @Nullable
    String getString(String key, @Nullable String defValue);

Are you sure it does not throw an exception here and would explain why you cannot see the log line?

I suggest you to use

boolean commit();

And check the return value. The apply method unfortunately commits its changes to the in-memory but starts an asynchronous commit to disk and you won't be notified of any failures.

like image 166
Damien Praca Avatar answered Jan 16 '23 15:01

Damien Praca


I think that you should use the context of the activity, not getApplicationContext()

Both of them are instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.

You can get the context of the activity related inside the fragment, for example like that:

private Context ctx;

 @Override
 public void onAttach(Context context) {
 super.onAttach(context);
 ctx = context;
 }

And then where you need it do:

preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
like image 30
Spirrow Avatar answered Jan 16 '23 15:01

Spirrow