Anthony Bonarrigo
Anthony Bonarrigo

Reputation: 180

SharedPreferences always returning null

I have an ActivityA that saves an user name to a SharedPreference and an ActivityB that is attempting to access the preference. However, it always returns null

Here's my ActivityA: (using MODE_WORLD_READABLE for testing)

// I know un is not null
String un = data.getExtras().getString("username");
Log.d("un value", un);
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("username", un);
editor.apply();

Here's ActivityB: (Log shows null value)

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String username = myPrefs.getString("username","");
Log.d("Username", "username from store: " + username);

After looking at many SO examples of using this class, I'm not sure what the issue is. Can anyone please help?

Upvotes: 0

Views: 627

Answers (3)

Reaz Murshed
Reaz Murshed

Reputation: 24211

Save your preference like this.

String un = data.getExtras().getString("username");

SharedPreferences myPrefs = getSharedPreferences("myPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("username", un);
editor.commit();

And retrieve the preference value like this.

SharedPreferences myPrefs = getSharedPreferences("myPrefs", Activity.MODE_PRIVATE);
String username = myPrefs.getString("username","");
Log.d("Username", "username from store: " + username);

Upvotes: 0

Jugal K
Jugal K

Reputation: 195

Check this :

This will help in maintaining separate class for preference. Use PreferencesUtil.class to deal with values.

Application extended class

public class MyApplication extends Application {

/**
 * The constant mApplication.
 */
public static MyApplication mApplication;
public static final String PREFERENCE_NAME = "PREFRENCE_FILE_NAME_1.0";
public static final int PREFERENCE_MODE = 0; //private mode

@Override
public void onCreate() {
    super.onCreate();
    mApplication = this;


}

/**
 * Gets application.
 *
 * @return the application
 */
public static MyApplication getApplication() {
    return mApplication;
}

public static SharedPreferences getSharedPreference() {
    return getApplication().getSharedPreferences(PREFERENCE_NAME, PREFERENCE_MODE);
}

}

PreferenceUtil.class

public class PreferencesUtil {

private SharedPreferences mSharedPreferences = MyApplication.getSharedPreference();
private int DEFAULT_INT= 0;

public PreferencesUtil() {

}

/**
 * Shared Preference.
 **/
// Save strings in preference
public void savePreferences(String key, String value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

// Save boolean values in preference
public void savePreferencesBoolean(String key, boolean value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

// Save boolean values in preference
public void savePreferencesLong(String key, long value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putLong(key, value);
    editor.commit();
}

// Save int values in preference
public void savePreferencesInt(String key, int value) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
}

// Get string values from preference
public String getPreferences(String key,String defaultValue) {
    return mSharedPreferences.getString(key, defaultValue);
}

// Get boolean values from preference
public boolean getPreferencesBoolean(String key) {
    return mSharedPreferences.getBoolean(key, false);   //false is default value
}

// Get Long values from preference
public long getPreferencesLong(String key) {
    return mSharedPreferences.getLong(key, DEFAULT_INT);   //false is default value
}

// Get int values from preference
public int getPreferencesInt(String key) {
    return mSharedPreferences.getInt(key, DEFAULT_INT);   //false is default value
}

}

Upvotes: 2

Tabish Hussain
Tabish Hussain

Reputation: 852

I think the issue is the MODE_WORLD_READABLE while putting data into the preference try using any other Mode Like MODE_PRIVATE

Upvotes: 0

Related Questions