Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix setDefaultValues crash #166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.Nullable;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceManagerFix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,6 +20,7 @@ public class MyPreferenceFragment extends PreferenceFragmentCompatDividers {

@Override
public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) {
PreferenceManagerFix.setDefaultValues(getPreferenceManager().getContext(), R.xml.settings, false);
setPreferencesFromResource(R.xml.settings, rootKey);

testDynamicPrefs();
Expand All @@ -30,7 +32,7 @@ public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String r

Preference prefEmptyCheck = findPreference("pref_empty_check");

if(prefEmptyCheck != null) {
if (prefEmptyCheck != null) {
prefEmptyCheck.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/test_list_entries.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="pref_list_entries">
<item>Alpha</item>
<item>Bravo</item>
<item>Charlie</item>
</string-array>

<string-array name="pref_list_values">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>

<string name="pref_list_default_value" translatable="false">B</string>

</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
android:title="ColorPickerPreference"
app:pref_size="small" />
<SimpleMenuPreference
android:defaultValue="1"
android:defaultValue="@string/pref_list_default_value"
android:entries="@array/pref_list_entries"
android:entryValues="@array/pref_list_values"
android:key="pref_simple_menu"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package android.support.v7.preference;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v4.content.SharedPreferencesCompat;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -100,4 +102,25 @@ private void setNoCommitFix(boolean noCommit) throws IllegalAccessException {

this.noCommit = noCommit;
}

@SuppressLint("RestrictedApi")
public static void setDefaultValues(@NonNull Context context, int resId, boolean readAgain) {

final String sharedPreferencesName = context.getPackageName() + "_preferences"; // PreferenceManager.getDefaultSharedPreferencesName()
final int sharedPreferencesMode = Context.MODE_PRIVATE; // PreferenceManager.getDefaultSharedPreferencesMode()

final SharedPreferences defaultValueSp = context.getSharedPreferences(KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE);

if (readAgain || !defaultValueSp.getBoolean(KEY_HAS_SET_DEFAULT_VALUES, false)) {
final PreferenceManagerFix pm = new PreferenceManagerFix(context);
pm.setSharedPreferencesName(sharedPreferencesName);
pm.setSharedPreferencesMode(sharedPreferencesMode);
pm.inflateFromResource(context, resId, null);

defaultValueSp.edit()
.putBoolean(PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES, true)
.apply();
}
}

}