Does android cache an application preference? - android

I would like to know does Android caches an application preference?
I have an activity running.
And then I go to Setting->Manage Application->find my application-> clear data.
But from my activity, the value of a preference in my application stays the same, despite I clear it thru the above step.
But then when I power off and then power on the phone, that seems to clear it.
My question is does android some how cache an application preference?
Thank you.

I presume you mean SharedPreferences by telling Application Preference..
Android clears the preference files when you do clear data..
The application data(the values read from the preference files before it was cleared) stays in the memory until the app is killed by the system, that might be the reason why you are seeing that the value of preference stays same..
Try Clear Data and Force Close.. That should give you what you are expecting..

Related

sharedpreferences remains when the app is closed

I'm saving some data into sharedpreferences and my problem is the data remains even when the App is closed, and even if the pda is reset
Another question about the lifecycle, my App should keep working in background and it does if i press the "menĂº" button of my pda, choose other apps in the meanwhile, etc... But if I press the "back" button, it executes the ondestroy method and the App doesn't keep working.
Thanks!
sharedpreferences are designed to do just that. persist data between app executions..
-- http://developer.android.com/guide/topics/data/data-storage.html#pref
if you want your app to remain active in the background you need to have it run as a service.
-- https://developer.android.com/training/run-background-service/create-service.html
The sharedpreferences are intented to stay when the app is closed. It's just an xml-file storing your data. If you don't need some of the values you either may reset them in the onCreate of your Base-Activity or define a global class that extends Application and store your data there in global variables.

Preferences between activity and service

I tried to find an explanation about this issue but I couldn't find anything.
I have a remote service that is working in the background. This service reads some preferences from a SharedPreference.
The problem I have happens when I change a preference from a PreferenceActivity (the activity and the service belong to the same application and the same package). I change the preference and it is saved correctly, but as soon as the service reads that preference (the service never modifies the preferences, it only modifies some internal preferences that cannot be modified from the PreferenceActivity) the preference is 'resetted' and the service gets the resetted value.
This is happening with CheckBoxPreference. I don't know if this would happen with other kind of preferences as I don't have any of them.
After I change the preference I should restart the service? Or I need to 'refresh' the preferences in the service? Maybe this is a problem related with using HoloEverywhere?
To get the SharedPreference object I use the following code:
PreferenceManager.wrap(context, getSharedPreferencesName(context), Context.MODE_MULTI_PROCESS);
This code is specific for HoloEverywhere and it equals this:
context.getSharedPreferences(getSharedPreferencesName(context), Context.MODE_MULTI_PROCESS);
'getSharedPreferencesName()' just returns a string composed by "package.name_preferences".
Thank you.
I have a remote service that is working in the background.
Why did you make your service be remote?
I change the preference and it is saved correctly, but as soon as the service reads that preference (the service never modifies the preferences, it only modifies some internal preferences that cannot be modified from the PreferenceActivity) the preference is 'resetted' and the service gets the resetted value.
That's because you made your service remote. Simply remove the android:process attribute from your manifest, to have all your components run in the same process, and this problem will go away. Along the way, you will make the user happier, because you won't be consuming as much RAM and battery.
After I change the preference I should restart the service? Or I need to 'refresh' the preferences in the service?
If you truly have a legitimate reason for having a remote service -- and IMHO there's a greater chance that I will spontaneously regrow my hair -- you will need to restart the service's process, AFAIK. SharedPreferences are cached per process, and I don't know of a way to force Android to reload SharedPreferences from disk except by restarting the process.
Or, you could not have a remote service. The choice is yours.

Using sharedPreferences to save application state

I was wondering about how saving variables work by using SharedPreferences inside onStop() function. I already managed to save and retrieve variables from a "file".
My question is: lets say i have x layouts in my app, and need to use different file names in my SharedPreferences. What happens with all these files when the app is uninstalled? Do we waste our memory all the time? Is there a point where all these files are getting deleted?
SharedPreferences are deleted when the application is uninstalled.
Edit: For completeness, they'd also be deleted when one performs a "Clear data" operation from the Application Manager.

Android - saving application state

I want to save application state to be able to restore it after another launch. Is is it better to use method onSaveInstanceState and save it to Bundle or to use SharedPreferences?
Thanks
It depends on your intention. Using the onSaveInstanceState() is only a reasonable solution if you want to ensure saving the state during configurations changes and other restarting events. In case you aim for a true saving of the application's state beyond the lifecycle of the application, you should consider using either the SharedPreferences or maybe even employ a database.
I may not have the same development chops as some of the other posters here (I've been seriously developing apps since July 2012), but I've found a solution that integrates SharedPreferences as well as the onSaveInstanceState().
My App has a splash screen activity that reads values from SharedPreferences and assigns them to the appropriate variables. Additionally, each Activity I make has its own onSaveInstanceState() method, and I commit all of the data I need to save to SharedPreferences there, in each and every Activity. Since onSaveInstanceState() is run before an App or Activity closes normally, it should back up data values under all normal circumstances.
It may not be the most code-efficient solution, especially in larger Apps with many Activities, but as far as my tests go it protects your app from data loss 99% of the time.
If a more experienced developer would like to chime in and confirm or deny this, I'm sure it'll enrich the question and answer.
I am sure onSaveInstanceState() is the better option.
Here it is already given a better explanation: Saving Android Activity state using Save Instance State

SharedPreferences are sometimes deleted

My app uses SharedPreferences (with the help of a PreferenceActivity) to store all settings. But sometimes all settings are deleted and all values are set back to default. Mostly on Android 3.x tabs.
On normal smartphones there are no problems.
I have observed that all settings are deleted when the application process is killed by Android.
I noticed this because the notification icon is no longer displayed.
I also wonder why the process is killed on tablets and not on smartphones. On my smartphone the notification icon never disappears.
My app also has a service, the main process and the service both read and write settings to the SharedPreferences.
I've sometimes noticed that all settings are cleared once the service writes to the SharedPreferences. But it does not happen every time.
Any ideas?
I realize that the SharedPreferences should not be used by different processes, but it works on all smartphones without problems!
I found out the reason why the settings disappear:
If two processes write data to SharedPreferences, it can happen that all SharedPreferences are reset to default values.
In my case all settings are cleared when the service write the first data to SharedPreferences.
Now, the service provides only read access to the SharedPreferences. The settings that are accessed by both processes writing are stored in a database.
SharedPreferences usually work great and as you mentioned without any problem on smartphones. It should also work on tablets. However it would be best to use sqlite or backing your shared prefs on a server for each users id data loss is a problem.
I have had the same issue.
All values from shared preferences become default if one value occasionally written with key = null. This is possible when you creating new constants using Eclipse, which inits them with null by default, and then you forgot to go to the beginning of the class and change constant to some meaningful value.
If you continue using the app, read operation of shared preferences will return valid values.
But when context is changed(for example, when activity killed by android and recreated) all values from shared preferences will return default values(zeroes).
So check your shared preferences's keys!
I hope this answer will help someone.

Categories

Resources