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

android:defaultValue ignored when using PreferenceDataStore #117

Open
Mygod opened this issue Nov 23, 2017 · 5 comments
Open

android:defaultValue ignored when using PreferenceDataStore #117

Mygod opened this issue Nov 23, 2017 · 5 comments

Comments

@Mygod
Copy link
Contributor

Mygod commented Nov 23, 2017

This is the decompiled Java code of Preference.dispatchSetInitialValue:

    private void dispatchSetInitialValue() {
        if(this.getPreferenceDataStore() != null) {
            this.onSetInitialValue(true, this.mDefaultValue);
        } else {
            boolean shouldPersist = this.shouldPersist();
            if(shouldPersist && this.getSharedPreferences().contains(this.mKey)) {
                this.onSetInitialValue(true, (Object)null);
            } else if(this.mDefaultValue != null) {
                this.onSetInitialValue(false, this.mDefaultValue);
            }

        }
    }

Unfortunately onSetInitialValue(true, defaultValue) doesn't work as expected. Also persisted attribute is ignored when data store is active. I wonder if there's a way to fix this in this library.

@gregkorossy
Copy link
Owner

What do you mean by "doesn't work as expected"?

Maybe the problems are related to this sentence in the documentation:

Once a put method is called it is full responsibility of the data store implementation to safely store the given values.

@Mygod
Copy link
Contributor Author

Mygod commented Nov 23, 2017

Oh it means that the default value passed is discarded and no default value is set. For example, the implementation of onSetInitialValue usually looks like this: (taken from ListPreference)

    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        this.setValue(restoreValue?this.getPersistedString(this.mValue):(String)defaultValue);
    }

You see when restoreValue is true, defaultValue is discarded.

@gregkorossy
Copy link
Owner

Can't decide whether it's intentional or Google messed it up. Did you report it on the issue tracker?

@Mygod
Copy link
Contributor Author

Mygod commented Nov 23, 2017

I reported but I'm too lazy to provide steps to reproduce for Google so they probably won't fix it.

Another evidence that it's Google messing it up, have a look at the signature of getPersistedString:

protected String getPersistedString(String defaultReturnValue);

Why would anyone pass this.mValue to defaultReturnValue?

@Mygod
Copy link
Contributor Author

Mygod commented Jul 12, 2018

Reading the source code of androidx, I think this has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants