I have two (the first and the last seen below) Switch Preferences in my XML defaulted to "true" and the rest "false."
XML
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Radar">
<SwitchPreference
android:defaultValue="true"
android:key="current_radar"
android:title="Current Radar"/>
<SwitchPreference
android:defaultValue="false"
android:key="current_satellite"
android:title="Current Satellite"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Maps">
<SwitchPreference
android:defaultValue="false"
android:key="fire"
android:title="Fire"/>
<SwitchPreference
android:defaultValue="false"
android:key="flood"
android:title="Flood"/>
<SwitchPreference
android:defaultValue="false"
android:key="hurricane"
android:title="Hurricane"/>
<SwitchPreference
android:defaultValue="false"
android:key="tornado"
android:title="Tornado"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Social Media">
<SwitchPreference
android:defaultValue="true"
android:key="twitter"
android:title="Twitter"/>
</PreferenceCategory>
</PreferenceScreen>
I want those two to show up as true, but they keep appearing as false every time I run my app.
Here is my
Class
package com.example.user.weatherdashboardandroid;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.view.MenuItem;
/**
* A {@link PreferenceActivity} that presents a set of application settings. On
* handset devices, settings are presented as a single list. On tablets,
* settings are split by category, with category headers shown to the left of
* the list of settings.
* <p>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
public class DataFeeds extends AppCompatPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new GeneralPreferenceFragment()).commit();
}
/**
* This method stops fragment injection in malicious applications.
* Make sure to deny any unknown fragments here.
*/
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
I have found other questions of similar nature, such as this which recommends using: `
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
inside the OnCreate of this class, my MainActivity, and the fragment shown above (context being getActivity() inside fragment's OnCreate) but none are working for me.
Randomly, the Fire switch shows up turned on even though its default is false. Is there some issue perhaps with having too many switches for one class?
How do I resolve my problem?