SharedPreferences are dissapearing after a few minutes. - android

I have a strange problem and I havent been able to find a solution to it.
My SharedPreferences are dissapearing after a few minutes I create a shared preference editor and then commit every change I make. I then get the SharedPreferences values to use using:
settings = getSharedPreferences("settings", 0);
if(settings.contains("accesstoken")){
// create a string that stores the accesstoken from our settings.
String str_access = settings.getString("accesstoken", null);
if(accesstoken_file_data != null){
str_access = accesstoken_file_data;
}
After a while the access token I stored along with all other data stored in SharedPreferences doesn't appear to exist... Thats if I leave my phone for a few minutes. I am developing with a HTC One X.
I am using this method to add values:
String accesstoken = "someVal";
SharedPreferences.Editor editor = settings.edit();
editor.putString("accesstoken", accesstoken);
editor.commit();
I have not found any viable solutions to this yet... if someone could help or direct me that would be great thanks...

SharedPreferences have an unfortunate tendency to wipe its data if the app crashes or is force stopped-
And all saving methods (with the exception of external storage) are wiped if the app data is cleared from settings
You could try to use a different saving method for storing the data. You have databases (SQLite), internal and external storage. The last two are file-based.

Related

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.

Does SharedPreferences work in Eclipse emulator?

I am attempting to save a user id using SharedPreferences. Do values saved as SharePreferences persist across all Activities in my application so I can access the userid from any application? Below is my code for saving the userid.
userid = result.substring(3, result.length());
Log.d("userid at onpostexecute", userid);
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); // to update userid
editor.putString("userid", userid);
editor.commit();
And here is the code for accessing the userid from the SharedPreferences in another activity.
SharedPreferences prefs = getPreferences(MODE_PRIVATE); // to access userid
String userid = prefs.getString("userid", "");
Log.d("shared prefs userid", userid);
What is strange is that the above code is in my onCreate method but it doesn't show up in the Logcat even though other log data is displayed before and after this code. So is there something wrong with my code that I can't even get it to display in my logcat? I can't even tell if it is being updated.
Values saved as sharedPreferences can be shared between activities if you tell it to. Right now you are creating a preference that is only accessible to that same activity. You need to use:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
Like this you are creating a sharedPreference between your application. There is a lot of explanation on the topic in the accepted answer to another question. Link to question
As discussed in the answer the way you are using to save the preference will only work in the same activity that saved it.
I assume you mean can you access them from any Activity, yes, they do persist even when you leave your app and come back. From the Docs
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
If this Log.d("userid at onpostexecute", userid); doesn't even show up then I would put a breakpoint there and make sure you have a value for userid. I would also check your logcat filters to make sure that you are getting all logs. Just make sure that the type in the spinner is set to "verbose" just to be sure

Best way to store a single user in an Android app?

I'd like my app to allow a single user to create a profile that is accessed every time they run the app. The user's profile contains a String entry and five int entries. I've thought about using an SQLite database to store the info between sessions, but it seems like overkill since I'd only need one row in a single table to store the data. However, I'm not sure if it's possible to use SharedPreferences on a String type of data.
Can anyone provide some insight into this issue please? Thank you!
Yes you can use SharedPreferences.
To write
Editor editor = getSharedPreferences("file_name", MODE_PRIVATE).edit();
editor.clear();
editor.putString("pref_name", myStringValue);
editor.commit();
To read back
SharedPreferences preferences = getSharedPreferences("file_name", MODE_PRIVATE);
String myStringValue = preferences.getString("pref_name", "default_value");
Regards.

Write File without MODE_WORLD_READ/WRITE

Okay, I have come across many tutorials on creating and writing/reading a file. What I want my Android app is to create a small text file where it will read and write some settings so my user information can be saved. The problem is every tutorial I have come across uses the SDcard (which allows the user and any other app to read) or use MODE_WORLD_READABLE which makes any app read off of it.
I was wondering how I can create and read/write my file but private or atleast keep it on the internal storage of the phone.
Here's an easy example. To read a preference do this:
SharedPreferences preferences = getSharedPreferences("YourAppName", MODE_PRIVATE);
String thisString = preferences.getString("KeyForThisString", "default");
You can getInt, getLong, etc.
To store the preferences, use these lines:
SharedPreferences preferences = getSharedPreferences("YourAppName",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("KeyForThisString", thisString);
editor.commit();
As before, you can putInt, putLong, etc.
The available methods for this API are here: http://developer.android.com/reference/android/content/SharedPreferences.html
dont know how to use it much and from what im seeing at least, its not much of a difference unless im wrong.
I still recommend to use SharedPreferences technique. It's explicitly easy and save a lot of time.
You can use it to save any primitive data: booleans, floats, ints, longs, and strings.
For example, you can do that each time when user invokes any changes to your Application because it's high performance technique. By this way even on crash all edited info will be stored.
Suppose you have an Activity.
Use this method where data is your String that you wanted to save to file.
private void saveInfo(String data){
String key = "myKey";
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(key , data);
editor.commit();
}
Now, when you launch your application again onCreate method invoked and you can load your information back:
private void loadInfo(){
String key = "myKey";
SharedPreferences mPrefs = context.getSharedPreferences(LauncherUI.PREFS_NAME, 0);
String yourData = mPrefs.getString(key, ""); // "" means if no data found, replace it with empty string
}
Here is a link to 5 types to store data: Storage Options

How exactly to save data in App Data Storage of applicationcraft?

I am newbie to Mobile development, I am using http://motech.applicationcraft.com/ to get some app going. I read about App Data Storage and wanted to use it to save some data.
I have done something like below.
3 text box with storage in d/b value TRUE.
Now I want on Click of Save button this 3 details gets saved.
My guess I need to write some Javascript on click of Save button, not sure how/what?
Pritesh
Internal storage in AC is deprecated so using d/b value= true will not do anything.
Store data securely into your own external database using Server Side functions (see this link )
Use SharedPreference to store data in App.
Like..
public static final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name" valueOfName);
editor.putString("name" valueOfUserId);
editor.putString("name" valueOfPassword);
editor.commit();

Categories

Resources