SharedPreferences not saved using PreferenceActivity - android

I'm trying to create a very basic preferences activity, extending PreferenceActivity. In the documentation, I read:
The PreferenceActivity automatically persists the settings associated with each Preference when the user makes a change.
However, when I:
Change a setting (any setting)
Close the app
Remove the app from the recently used list
Relaunch the app
The setting is reset to the default value, which seems to conflict the quoted documentation above. Do I misunderstand something here, is the template faulty, or do I perhaps need additional permissions or other settings to use the PreferenceActivity auto-persist feature?
public class SettingsActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
And preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="pref_language_reading"
android:title="#string/pref_title_language_dailyreading"
android:entries="#array/pref_languages_dailyreading_entries"
android:entryValues="#array/pref_languages_dailyreading_values"
android:defaultValue="#string/pref_language_dailyreading_default"
android:persistent="true" />
</PreferenceScreen>
The strings and arrays exist, and I can select a language properly. The setting is saved when I go to another activity, and then back. But closing the app and relaunching discards the saved value.
When I read out the SharedPreference corresponding to this preference (before relaunching), I see the correct setting. After relaunching, the SharedPreference does not exist anymore though.

The code should work. There is likely something else in your code that causes this behaviour.
In my specific case, it turned out there was a line
PreferenceManager.getDefaultSharedPreferences(this).edit().clear().commit();
In the onCreate() method of the main Activity, causing the SharedPreferences to clear on every launch of the app.

Related

how to turn on SwitchPreference programatically on Android

I've a settings fragment which load a xml with a default settings page, I also added one click listener to one specific preference
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
preferences.findPreference(getString(R.string.pref_custom_list)).setOnPreferenceChangeListener(this);
//other stuff.....
this works fine, when user clicks triggers an event and i can check some info about the switch including deny the change...
But i would like to turn on/off other switches in the same screen when this even happen
i tried to
preferences.findPreference(getString(R.string.xpto)).setEnabled(true);
but it doesn't turn any switch on or off... it just set the view enabled or disabled for clicks
if i do something like
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("xpto",true).apply();
It does change the preference, but the changes are not loaded to the screen so user doesn't know
how can i switch some preference on or off programatically and make it reflect to the preference screen
You can use SwitchPreferenceCompat.
<SwitchPreferenceCompat
app:key="key"
app:title="Some Confs" />
Try this in your fragment if you are using SwitchPreferenceCompat as above.
SwitchPreferenceCompat switchPref = findPreference("key");
switchPref.setChecked(true);
You're looking for setChecked(), not setEnabled():
preferences.findPreference(getString(R.string.xpto)).setChecked(true);
I think you need to implement a listener when the SharedPreferences change.
SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// change the Switch Preference state here
}
};
Then when the prefs change, you can change the user's screen to match it.
For your reference:
How to detect if changes were made in the preferences?

How to access checkbox value in a settings activity from another activity?

I've created a new settings activity using the wizard in Eclipse. I would like to know how to access the value that reflects the user's choices. In particular, a checkbox option.
The code it has produced is as follows:
/src/com/example/myapp/SettingsActivity.java:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupSimplePreferencesScreen();
}
private void setupSimplePreferencesScreen() {
addPreferencesFromResource(R.xml.pref_general);
//...
}
res/xml/pref_general.xml:
<CheckBoxPreference
android:defaultValue="true"
android:key="example_checkbox"
android:summary="#string/pref_description_social_recommendations"
android:title="#string/pref_title_social_recommendations" />
The wizard creates quite a large example, so I'm not sure if I've copied/pasted all the relevant code, but I have spent quite a long time and I'm pretty sure this is about it, (besides the string xml file).
How can I access the checkbox's value from another activity? I'm guessing I need to use a SharedPreferences object, but I don't know what keys I would use to access the data, as I can't find any constants in the code for that? Thanks
PreferenceManager.getDefaultSharedPreferences(context).
getBoolean(example_checkbox, defaultValue);
The key you use is the one you sepcified in the android:key field

Preference in PreferenceFragment is not getting updated

I have a main activity that loads a PreferenceFragment (its part of an actionBar).
Within the PreferenceFragment I load my preferences from a XML File:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
The Problem is if I change the Tab (remember it is getting managed by an ActionBar), change one of the preferences in a different fragment and coming back to my preferenceFragment its not getting updated. Particularly the problem is a SwitchPreference(true/false) which can be changed from different fragments and even by remote calls (then the preference is getting changed in shared preferences. YES I did commit the change).
I searched for different solutions, but to be honest I didn´t find a working one.
My own ideas are the following:
My Problem would be solved if I could get the Switch Element of the switch, so I could set the switch to true in the onStart() or onResume() method. But is there a change to get the Switch Object?
If I would load a usual layout I could access the switch like this:
View v = inflater.inflate(R.layout.start_fragment, container, false);
Switch status = (Switch) v.findViewById(R.id.switch1);
Afterwards I would be able to set the Position of the Switch like this:
status.setChecked(true);
Another solution would be to destroy the actual view and call addPreferencesFromResource() again, but to be honest I have no idea how this could be done....
The third solution could be to use a OnCheckedChangeListener in my PreferenceFragment, but the problem would again be how to change/update the switch.
I am sure that the Preference is updated correctly, because I´m running a OnSharedPreferenceChangeListener in one of my services and to debug this problem I made the listener log the status of my switchPreference.
Can you help me somehow?
unfortunately I cannot answer my own question before tomorrow morning, so I edit my question:
Thank you guys, I don´t want to restart the underlying activity, but only the fragment.
Fortunately I found a clue in this thread: How do you refresh PreferenceActivity to show changes in the settings?
I can really access the SwitchPreference that simple.
My solution is:
private SwitchPreference pref_phone_locked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
status = (SwitchPreference) findPreference("status");
}
public void onStart(){
super.onStart();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
if(sharedPref.getBoolean("status", false)){
status.setChecked(true);
}else{
status.setChecked(false);
}
}
This way I can simply cast the Preference to a SwitchPreference and modify it after every onStart() call. I guess this is the best solution for my problem.
Hopefully this answer will save someones time in future =)
Thanks you guys again!
I'm not sure if this will work for you ( I've a very basic knowlege of Android ) but I found this solution in a similar question and it works very well for me
private void simulateRefresh(){
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
Basically this destroy and recreate the activity without animation in between.
Found here

How to Initialize Preferences Screen on First Invocation?

I believe I am correctly initializing preferences from XML. My Preferences Screen also works properly and reflects the correct user selected settings.
However, upon first invocation of that Preferences Screen, none of the settings are checked (checkbox) or selected (list). This, of course, confuses the user as it doesn't reflect the current (default/initial) value.
Since all I do to invoke the Preferences Screen is
startActivity(new Intent(this, EditPreferences.class));
And my EditPreferences class only contains:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.usersettings);
}
I am not sure where or how to tell it to pre-initialize the visual display with the default setting.
I have this hunch that all I am missing is a single line somewhere, but I don't know where: XML file? override a method in EditPreferences? Other?
Can't you define the default value in the XML itself?
<CheckBoxPreference ...
android:defaultValue="true"
... />
You can specify a default value on a preference (in your xml layout for example):
<EditTextPreference android:defaultValue="whatever" />

Is it possible to display a DialogPreference without showing a PreferenceScreen?

I'm currently writing a live wallpaper for Android and it has a PreferenceScreen which currently contains only one preference - a DialogPreference to set various properties of animation.
User workflow to configure it currently looks like this:
Settings... => (shows the preferences list with only one title ) Animation speed => MyDialogPreference
What I want is to make the workflow like this:
Settings... => MyDialogPreference
I.e. I'm looking for a way to skip showing that preferences list with only one item and to show that dialog right away.
But it seems that PreferenceActivity requests itself to have PreferenceScreen as a root element of preference hierarchy. So... is it even possible to do what i want? :)
Code references:
Activity code:
public class ForestLakePreferences extends PreferenceActivity
{
protected void onCreate(Bundle savedState)
{
super.onCreate(savedState);
getPreferenceManager().setSharedPreferencesName(
ForestLakeWallpaper.PREFS_NAME);
addPreferencesFromResource(R.xml.preferences);
}
}
Prefs resource:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="lake_preferences">
<DurationEditDialog
android:title="#string/prefs_duration_title"
android:dialogTitle="#string/configure_durations_dlg_title"
android:dialogLayout="#xml/set_durations_layout" />
</PreferenceScreen>
It turned out this can't be done in that way.
But i've found a workaround: i made my activity not a PreferencesActivity, but a custom one and made it look like dialog by placing the following in AndroidManifest.xml
<activity android:theme="#android:style/Theme.Dialog" .... />
A DialogPreference has a showDialog(Bundle state) method, try calling it. I am not sure if you will have to give it anything else like the Preferences or anything.

Categories

Resources