PreferenceManager.setDefaultValues readAgain parameter and new preferences - android

I'm using PreferenceManager.setDefaultValues(context,R.xml.preferences,false) to set the default values of the preferences in my application and I've noticed that if I add a new preference then my PreferenceFragment does not show a default value for it, even if it is set in the preferences.xml..
Is this due to the fact that I specify readAgain as false?
I know I can simply try-and-see, but I just wanted to be sure that the readAgain is meant for this purpose, i.e. re-read the default values not to re-set user settings but to set the new properties' default values. If this is exactly the purpose of the readAgain flag, then can you give me a reason not to always set readAgain to true, given that it does not reset user preferences?

Setting the readagain flag to true will ensure that all unset-default values are set again. That's the summary.
Setting the readagain will cause extra overhead since basically it means that, the code will go through the entire SharedPreference once more to check them. With the readAgain false,all it does is to check whether the file ( has_set_default_values.xml ) which is used as a marker for setting default values is present or not.
In any case, It won't set any values that are already set .

Related

Shared preferences isn't gettingBoolean properly

I'm trying to load boolean value from Shared Preferences. On first launch it should be TRUE as I have never saved it to Shared Preferences. However, I'm somehow getting false.
See code below.
settings = context.getSharedPreferences(SAVED_PREFERENCES, 0);
isFirstLaunch = settings.getBoolean(FIRST_LAUNCH, true);
Log.d(TAG, "loadIsFirstLaunch: " + isFirstLaunch);
p.s. I haven't saved FIRST_LAUNCH value in shared preferences before 100%.
UPD1 I've double checked with empty project - code is working properly. Shared Preferences use Boolean class, not boolean primitive type. So it isn't the case that boolean by default is false. Also according to debug my shared preferences somehow includes FIRST_LAUNCH value by the time it is launched. So looking at how it got there.
UPD2 I finally stuck. Removed all mentionings of FirstLaunch, but it still somehow appears in SharedPreferences when another call to read other values is made. So question is - how can I get rid of saved value in Shared preference.
http://take.ms/Rr0Xf
UPD3 I've changed name to my saved preferences file and it worked. So problem was that SOMEHOW device was keeping info for saved preferences even after application clean install. PFM.
I can't commnet to your question because of less points So, if you're 100 sure that you didn't set any value to FIRST_LAUNCH, then 1. try to clean the app's data 2. Can you please provide the full source code
And #miller what if there are more than one sharedpreferences
Its because the boolean value by default sets its value to false .. it only takes the true value when there is empty value returned from the shared preference , but boolean value must have a value either true or false...it doesn't support null value.
so what you need to do is set the boolean value of the session to true programatically in the class before loading and then get the value from the session
So finally I found the problem and solution.
In android M and above versions Google keeps application backups in google driver, that's why even after reinstall shared preferences were restoring. I've disabled backups.
project manifest file under Application section set android:allowBackup="true" to false.

In which case should I set the boolean parameter of onLayoutFinished as false?

I am working on print custom document in Android, in PrintDocumentAdapter I need to override the function onLayout, I should call onLayoutFinished, I always set the second parameter as true, but the website says
Note: The boolean parameter of the onLayoutFinished() method indicates whether or not the layout content has actually changed since the last request. Setting this parameter properly allows the print framework to avoid unnecessarily calling the onWrite() method, essentially caching the previously written print document and improving performance.
So how can I know whether the layout has been changed from last request, and if it false does it mean it is the same layout with the former layout so why onLayout is called? Which action triggers? I am new to android, so any help is appreciated.
Since you're using custom printing, your app determines how the content is laid out and can determine if the file needs to be re-written hence setting it to true. If the content of the file hasn't changed based on the new 'PrintDocumentInfo' then you can set it to false.

Why not use PreferenceManager.setDefaultValues(readAgain = true) in all apps?

Android Settings guide suggests to call PreferenceManager.setDefaultValues() with readAgain = false. In this case the defaults from preferences.xml are only loaded once when the app is launched for the 1st time (or after "clear data").
If a new preference is added, its android:defaultValue is not loaded, I checked.
So why nobody uses readAgain = true? Google results:
10 "PreferenceManager.setDefaultValues(this, R.xml.preferences, true)"
60k "PreferenceManager.setDefaultValues(this, R.xml.preferences, false)"
What are the drawbacks?
This parameter is typically set to false because this one ensure that default values are only set for shared preferences that are not initialized. Keeping this to false will ensure that you do not override the setting modified by the user. This field is useful if you allow your user to re-set all app settings to the initial app settings.

How can I force SharedPreferences to save?

As far as I can tell, values aren't stored in Android's SharedPreferences until they're explicitly accessed. That is, while they may have default values in XML, no value is placed in a SharedPreferences store until an accessor method is called, which is why all the accessors have "default" parameters included.
While this isn't a huge deal for simply pulling values out of the preference store, it prevents any attempt to get all the preference keys that are used in the application, even if they are stored in XML. The keys don't appear when SharedPreferences#getAll() is called unless the preference has already been explicitly accessed.
Is there any way to force all preferences defined in XML to be saved into a SharedPreferences store? The nearest solution I can find is to manually parse the Preference XML files, find all keys and defaults, and save the default value for each one. Is there a cleaner approach?
UPDATE
After looking at this in more depth, I've been getting a partial list of preferences for a different reason. When the defaults are set, only EditTextPreference and ListPreference values are saved. The other two, a custom preference and a CheckBoxPreference, are completely ignored. Here's an example of the CheckBoxPreference that's being ignored:
<CheckBoxPreference
android:defaultValue="false"
android:key="PREF_NAME"
android:summary="Summary text"
android:title="Title" />
Any idea why not all defaults are being set?
You can use PreferenceManager::setDefaultValues. For instance, at your Application::onCreate method.
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
If the last argument is false, this will only set the default values if this method has never been called in the past.
The reason for the missing preferences in my case was actually an Android bug. The workaround was to manually set the missing preferences to their default values, as indicated in a duplicate question: Android CheckBoxPreference Default Value

Setting Boolean Preference default value depending on device langage (Locale)

I currently have an application where the french users should have a preference set to false by default and the other one should have this preference set to true.
I don't understand a correct and clean way to handle that.
Currently , in my app, I call 2 times the preference.
*) first time in my preference xml layout and in my preference activity, I have to set the preference at the first launch.
*) Second time, in my code:
boolean value = prefs.getBoolean("key"), true/false);
Thanbk a lot for all your ideas and explanations on how to make this in a clean way.
You can use resources in such a way to accomplish this.
Folder structure (you can probably pick whatever name you want for the actual XML file or even create the resource with other resources):
/res/values/bools.xml
/res/values-fr/bools.xml
In /res/values/bools.xml:
Make the boolean, you can rename this to whatever you need.
Name: pref_default
Type: boolean
Value: true
In /res/values-fr/bools.xml:
Make the boolean again, same name
Name: pref_default
Type: boolean
Value: false
In your preferences.xml:
Set the default value to #bool/pref_default
All set!
You should even be able to access that from code with R.bool.pref_default.
Disclaimer: I have never written code that involved multiple languages, but I have based this on my understanding of resource qualifiers.
If you mean the region France and not the French speakers, then you can probably find a -r qualifier for it (see my link).

Categories

Resources