Multiple object of shared preference deletes data - android

I'm using shared preferences throughout my application. I'm having a problem though. I think my data is being lost on background.
SharedPreferences preferences = getSharedPreferences(pref_data, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("isDriverLogin", "True");
editor.putString("driverPassword", driverPassword);
editor.putString("carrierId", carrierId);
editor.putString("CCTID", cctid);
editor.putString("shipment", entityShipment);
editor.putString("isAccepted", "");
editor.commit();
And somewhere in the app I create another object and edit just one portion of the data
SharedPreferences preferences = getSharedPreferences(pref_data, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("isDriverLogin", "True");
editor.commit();
I'm doing this throughout my application. Editing just one or two data. But at some point my app crashes or the expected behavior is not happening. This behaviors are dependent on my preference data.
I tried fixing it by just creating one object of share preferences and always edit them with all the data. For example I have 5 data on shared preferences. If I just need to edit one, I will still put some data on the other 4 just to make sure nothing is being overwritten.
Do you guys have any idea?

Related

How secure Shared Preferences data in my application from Security Auditor

How to secure SharedPreferences data in my application?.
I use 2 step for this but in Security Audit hacker are able to hack my data.
1 Step-
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", value);
editor.commit();
2 Step- Use SecureSharePreferences
SecurePreferences securePrefs = new SecurePreferences(context, "key", "my_user_prefs.xml");
SharedPreferences.Editor editor =securePrefs.edit();
editor.putString(key, value);
editor.commit();
Any other method to handle this.
After read some Answer I update my code with encrypt data but problem still exist.Security auditor still getting application sharedpreference.file from app memory.
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
String keyEncript = EncriptionDecriptionUtils.encriptionOfData(key).toString().trim().replaceAll("\r\n", "");
String value = sharedPreferences.getString(keyEncript, "").trim().replaceAll("\r\n", "");
String valuedecript = EncriptionDecriptionUtils.decriptionOfData(value).toString().trim().replaceAll("\r\n", "");
return valuedecript;
You can encrypt & decrypt the shared preferences data using AES algorithm.If you open shared preferences explicitly you will get encrypted information only.For your reference look into this Securing SharedPreferences Data using AES algorithm
On a rooted phone, it can access the shared preferences for your app. Also, on any phone the user can delete all the data that it's stored in shared preferences by clearing the cache in the application manager. A safe way to store data would be to encrypted it with AES and save it in a text file in the root folder of your app.

Add integer value to Shared prefference

Im creating a alarm app that will have multiple alarm.. so i want to store the times in shared preference. if there is any solution to store integer list in shared preference
SharedPreferences preferences = getSharedPreferences("prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("key", yourIntValue);
editor.apply();
Use editor.apply() to save preferences asynchronously. apply() is preferable in the most cases.
Use editor.commit() to save preferences synchronously.

how to append data in Shared Preference in Android

I tried to append the data in shared preference file by using
SharedPreferences sharedPreferences = getSharedPreferences("myData", MODE_APPEND);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", userName.getText().toString());
editor.putString("password", password.getText().toString());
editor.commit();
But I found that new value overwrites the old value. Will you help me to fix this issue?
MODE_APPEND doesn't mean that you add multiple values for each key. It means that if the file already exists it is appended to and not erased . We usually used MODE_PRIVATE.
As for saving multiple names and passwords, you can take a look at putStringSet(string key Set<String> values Method.
You can save the for each key a set of string values. You can separate the username and password by some special character or string. You may even serialize an object to json.
So basically what you need to do is:
Get the list of values from Shared Preferences
Append the current value to the list.
Save the List back to Shared Preferences.

android cordova sharedpreference deleting during force close

We are facing an issue with android SharedPreferences in our application. Our application is a cordova application. We are storing few data in the SharedPreferences. For example, storing userid, last visited page html contents, few json values.
Following are code samples for managing sharedpreferences (get/set/remove).
//get
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
Object obj = sharedPrefs.getAll().get(key);
//remove
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
if (sharedPrefs.contains(key)) {
Editor editor = sharedPrefs.edit();
editor.remove(key);
}
//store
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cordova.getActivity());
Editor editor = sharedPrefs.edit();
editor.putString(key, value);
editor.commit();
The storing retrieving deleting everything working fine. The problem we are facing is, the data is not persisting in a consistent manner.
For example, user opens the app and then he force close the app, again takes the app. All data in the sharedpreference is persists. But when he force close 2nd time and come back to the app once again, then the data from the sharedpreference is deleted. I could not find any log in the logcat also. Any help will be much appreciated.

Preserve data on text field between calls on different activities in the same app

I have a app that i still building, tha app has 4 activities. The number 1 has 2 texts fields. I call other activity, eg 2, when call activity number 1 back, without destroying the app, i lose every info that was in those fields. How to preserve this data. If the app is closed (destroyed) i don't want this data to be preserved. This data is not to be shared with other activities.
you can use shared preference to save data .
save text :
SharedPreferences.Editor editor = context.getSharedPreferences("GLOBAL_PREF", 0).edit();
editor.putString("text1", Value);
editor.commit();
get text :
String text1 = context.getSharedPreferences("GLOBAL_PREF", 0).getString("text1", null);
clear preference :
SharedPreferences.Editor editor = context.getSharedPreferences("GLOBAL_PREF", 0).edit();
editor.clear();
editor.commit();
clear single key :
SharedPreferences.Editor editor = context.getSharedPreferences("GLOBAL_PREF", 0).edit();
editor.remove("text1");
editor.commit();

Categories

Resources