Android preferences do not seem to be persisted between application starts - android

I'm using preferences to save some user settings in my Nexus 7 app. My code for saving a value to preferences is:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String systemId = spinnerActivity.getSelectedItem().toString();
editor.putString(PreferenceKeys.SAVED_SYSTEMID, systemId);
if (!editor.commit()) {
Toast.makeText(getApplicationContext(), "Error saving System ID", Toast.LENGTH_LONG).show();
}
I've stepped through this with the debugger and it is being called correctly. When I restart my app. and try to read back the value with the code below, I always get null.
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String savedSystemId = sharedPref.getString(PreferenceKeys.SAVED_SYSTEMID, null);
ConnectionInfo.setSystemId(savedSystemId);
The loading is called from the onCreate() function in the main activity. Strangely enough loading of other preference values elsewhere in the app. works fine, it's just this one case that doesn't work. Can anyone see what's wrong?

SharedPreferences sharedPref = getSharedPreferences("Name_of_item",Context.MODE_PRIVATE);

please try the below
SharedPreferences sharedPref = getPreferences("preference_name",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String systemId = spinnerActivity.getSelectedItem().toString();
editor.putString(PreferenceKeys.SAVED_SYSTEMID, systemId);
if (!editor.commit()) {
Toast.makeText(getApplicationContext(), "Error saving System ID", Toast.LENGTH_LONG).show();
}
SharedPreferences sharedPref = getPreferences("preference_name",Context.MODE_PRIVATE);
String savedSystemId = sharedPref.getString(PreferenceKeys.SAVED_SYSTEMID, null);
ConnectionInfo.setSystemId(savedSystemId);

Instead of using:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
Try to use this:
import android.preference.PreferenceManager;
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
Or, you can use a named preferences page in which case:
public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences sharedPref = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

Related

default sharedpreferences wont share between activities

i am trying to pass a string set between activities by sharedpreferences but it seems that the default sharedpreferences made two files for each activity.
i tried to share it with PRIVATE_MODE with the same name and it didnt work
SharedPreferences appPrefernces = PreferenceManager.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor appEditor = appPrefernces.edit();
Set<String> usersSet = appPrefernces.getStringSet("users", new HashSet<String>());
if(!usersSet.contains(id)) {
usersSet.add(id);
appEditor.putStringSet("users", usersSet);
appEditor.apply();
}
SharedPreferences appPrefernces = PreferenceManager.getDefaultSharedPreferences(Main.this);
users = appPrefernces.getStringSet("users",new HashSet<String>());
it seems that it saved the info but while extracting it i get a get just a partial set
In this way you are sure what preferences file you are using:
private static void saveUsers(Context context, Set<String> usersSet) {
final SharedPreferences sharedPreferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet("users", userSet).apply();
}
private static Set<String> loadUsers(Context context){
final SharedPreferences sharedPreferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
return sharedPreferences.getStringSet("users", new HashSet<String>());
}
Hope it helps.

SharedPreferences is not saving data

I'm trying to save a string in a key "password". I intend to save the data when the app is closed. So I tried to put the data & save it in onStop() method. But it's not working.
protected void onStop() {
SharedPreferences prefs = this.getSharedPreferences(
"gmail.nextgenancestor.knocked", Context.MODE_PRIVATE);
prefs.edit().putString("password", password);
prefs.edit().commit();
Log.d("password", password);
Log.d("goodbye", prefs.getString("password", "not found"));
super.onStop();
}
Result from the log:
password: EMERGENCY
goodbye: not found
The problem is that prefs.edit() creates an instance of Editor that you must re-use when you commit. This should fix the code:
SharedPreferences prefs = this.getSharedPreferences(
"gmail.nextgenancestor.knocked", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("password", password);
editor.commit();
the problem is that you are setting data in an instance of the SharedPreferences.Editor which you didn't call commit() on it . try this :
SharedPreferences prefs = this.getSharedPreferences("gmail.nextgenancestor.knocked", Context.MODE_PRIVATE);
prefs.edit().putString("password", password).commit();
or :
SharedPreferences prefs = this.getSharedPreferences("gmail.nextgenancestor.knocked", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("password", password);
editor.commit();
Here is a tutorial about how to use SharedPreferences in Android
Replace
SharedPreferences prefs = this.getSharedPreferences(
"gmail.nextgenancestor.knocked", Context.MODE_PRIVATE);
prefs.edit().putString("password", password);
prefs.edit().commit();
with
SharedPreferences prefs = this.getSharedPreferences(
"com.example.dsfsdf", Context.MODE_PRIVATE);
Editor edit = prefs.edit();
edit.clear();
edit.putString("password", password);
edit.commit();

About another Application to visit SharedPreference

I need visit the sharedPreference of another application, the SharedPreference have to set
"MODE_WORLD_WRITEABLE|MODE_WORLD_READABLE" , and show you the Logcat.
03-23 13:54:45.038: E/ApplicationContext(10895): Couldn't rename file /data/data/com.mzw.gamehelper/shared_prefs/public.xml to backup file /data/data/com.mzw.gamehelper/shared_prefs/public.xml.bak
I dont know what's wrong with it, who can help me, thank you.
try this code insert values
private SharedPreferences myPrefs;
myPrefs = Actionactivity.this.getSharedPreferences("myPrefs", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Mobile_no", getText_no.getText().toString().trim());
prefsEditor.commit();
and try this code get values
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
myPrefs.getString("Mobile_no", "");
TRY THIS WAY ::
Context otherAppsContext = null;
private SharedPreferences sharedPreferences;
public static final String PREFS_READ = "PREFS_READ";
public static final String KEY_READ = "KEY_READ";
try {
//com.dfdf.fdfdf.android.sharedpreferences OTHER APPLICTION PACKAGE NAME
otherAppsContext = createPackageContext("com.dfdf.fdfdf.android.sharedpreferences", 0);
} catch (NameNotFoundException e) {
}
sharedPreferences = otherAppsContext.getSharedPreferences(PREFS_READ, Context.MODE_WORLD_WRITEABLE);
String strvalue=sharedPreferences.getString(KEY_READ, "WORLD READ EMPTY")

How to use SharedPreferences to store persistant data Android

I am building a camera application. I'm trying to save some information using SharedPreferences.
I want to save some persistant information-the last image taken filepath. But the first time the application is used before taking a picture, the data would be NULL.
So I want to getSharedPreferences in onCreate and check if the value is null. But as far as I know the only way to use getSharedPreferences is only if you have called put on the Editor before. Hence, I am getting a NULL pointer exception on the SharedPreferences object the first time.
How do you resolve this?
//inside on Create()
imageData = getSharedPreferences("ImageData",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = imageData.edit();
prefEditor.commit();
String previousImage = imageData.getString("lastImageTaken", null);
if(previousImage == null){
Log.d(TAG,"previous image is NULL");
}
else{
//do something with the filepath
}
//-----------------------
//in onClick of capture button
imageData = getSharedPreferences("ImageData",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = imageData.edit();
prefEditor.putString("lastImageTaken", MyActivity.this.pictureFilePath);
prefEditor.commit();
Please try this
To read from SharedPreferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String name = preferences.getString("name","default");
String email = preferences.getString("email","default");
To save into SharedPreferences
Editor edit = preferences.edit();
edit.putString("name", "Roy");
edit.putString("email", "roy#mail.com");
edit.commit();

how to implement authentication using shared preferences in android?

I am new to Android.my requirement is to implement simple authentication logic for login screen by using sharedpreferences in android.
can any one suggest me...?
To save details after registration of user (when user is created)...
// Get the app's shared preferences
SharedPreferences login_app_preferences = context.getSharedPreferences("LOGIN_DETAILS", MODE_PRIVATE);
// Update fields
SharedPreferences.Editor editor = login_app_preferences.edit();
editor.putString("email", strEmailOrLoginId);
editor.putString("password", strPassword);
editor.commit(); // Very important
To access it any where in application....
// Get the app's shared preferences
SharedPreferences login_app_preferences = context.getSharedPreferences("LOGIN_DETAILS", MODE_PRIVATE);
strUserName = login_app_preferences.getString("email", "");
strPassword = login_app_preferences.getString("password", "");

Categories

Resources