Status Update
Comments
vi...@google.com <vi...@google.com>
vi...@google.com <vi...@google.com> #2
Please provide sample project or apk to reproduce the issue. Also mention the steps to be followed for reproducing the issue with the given sample project or apk.
Expected output
What is the expected output?
Current output
What is the current output?
Android bug report capturing
After reproducing the issue, press the volume up, volume down, and power button simultaneously. This will capture a bug report on your device in the “bug reports” directory. Attach the bug report file to this issue.
Alternate method
After reproducing the issue, navigate to “developer settings”, ensure “USB debugging” is enabled, then enable “Bug report shortcut”. Capture bug report by holding the power button and selecting the “Take bug report” option.
Screen Record of the Issue
Please capture screen record or video of the issue using following steps:
adb shell screenrecord /sdcard/video.mp4
Subsequently use following command to pull the recorded file:
adb pull /sdcard/video.mp4
Attach the file to this issue.
vi...@google.com <vi...@google.com> #3
vi...@google.com <vi...@google.com> #4
to...@yahoo.com <to...@yahoo.com> #5
hitting this same issue.
My code:
public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
@Nullable final String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
setPreferencesFromResource(R.xml.preferences_style, rootKey);
pTextScale = findPreference(TextScale.PK_TEXT_SCALE);
pTextScale.setPreferenceDataStore(styleDataStore);
Here is a debug trace:
java.lang.Thread.State: RUNNABLE
at androidx.preference.Preference.getPersistedInt(Preference.java:1809)
at androidx.preference.SeekBarPreference.onSetInitialValue(SeekBarPreference.java:207)
at androidx.preference.Preference.onSetInitialValue(Preference.java:1634)
at androidx.preference.Preference.dispatchSetInitialValue(Preference.java:1607)
at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1330)
at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1345)
at androidx.preference.PreferenceGroup.addPreference(PreferenceGroup.java:253)
at androidx.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:173)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:350)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:351)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:161)
- locked <0x5848> (a java.lang.Object[])
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:112)
at androidx.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:217)
at androidx.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:380)
at com.hardbacknutter.nevertoomanybooks.settings.styles.StyleFragment.onCreatePreferences(StyleFragment.java:101)
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:161)
The library code:
protected int getPersistedInt(int defaultReturnValue) {
if (!shouldPersist()) {
return defaultReturnValue;
}
PreferenceDataStore dataStore = getPreferenceDataStore(); <== still NULL of course
if (dataStore != null) {
return dataStore.getInt(mKey, defaultReturnValue);
}
return mPreferenceManager.getSharedPreferences().getInt(mKey, defaultReturnValue);
It's easy to see that the call to setPreferencesFromResource() ends up using the value from SharedPreferences and not from my datastore. But I can't set a PreferenceDataStore until that call is done.
Conclusion: when using setPreferencesFromResource, you simply cannot use PreferenceDataStore. I presume no one tested this?
I don't fancy building my preference screen totally from code just to be able to set the PreferenceDataStore. In that case I might as well just dump the whole preference library.
any thoughts?
and please do not say "migrate to the new datastore/kotlin/flow" - I'd have to dump the preference library as well to do that + add the mess called 'kotlin' ; no thanks.
to...@yahoo.com <to...@yahoo.com> #6
sorry, realized I should open new bug - done: 232206237
Description
Version used: 1.1.0
Theme used: Not relevant
Devices/Android versions reproduced on: API 28
The switchPreference will always show the default and will ignore the custom PreferenceDataSotre value
because Preference.onSetInitialValue is called after setPreferencesFromResource(R.xml.settings, null) and before setting the custom PreferenceDataStore
- Relevant code to trigger the issue.
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, null)
//Preference.onSetInitialValue is already called
preferenceScreen.findPreference<SwitchPreferenceCompat>("dark_theme")?.preferenceDataStore = object : PreferenceDataStore() {
override fun putBoolean(key: String?, value: Boolean) {
uiSettings.setDarkTheme(value)
}
override fun getBoolean(key: String?, defValue: Boolean): Boolean {
return uiSettings.getDarkTheme(defValue)
}
}