gregkorossy / Android-Support-Preference-V7-Fix

Android androidx.preference support library has some issues, this lib tries to fix them.
https://discord.gg/87NVsSK
Apache License 2.0
497 stars 46 forks source link

When start strict mode and set `android:persistent` to `true` will has error #160

Closed tcqq closed 6 years ago

tcqq commented 6 years ago

@Gericop When start strict mode and set android:persistent to true will has error. How can I fix this error? Thanks.

Application

override fun onCreate() {
    if (BuildConfig.DEBUG) {
        setStrictMode()
    }
    super.onCreate()
}

private fun setStrictMode() {
    StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
            .detectDiskReads()
            .detectDiskWrites()
            .detectNetwork()   // or .detectAll() for all detectable problems
            .penaltyLog()
            .build())
    StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects()
            .penaltyLog()
            .penaltyDeath()
            .build())
}

preference_example.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

        <SwitchPreference
            android:key="switch_preference_night_mode"
            android:persistent="true"
            android:title="@string/theme_settings_night_mode" />

</PreferenceScreen>

PreferenceFragment:

public class ThemeSettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preference_example, rootKey);
    }
}

Error log:

StrictMode: StrictMode policy violation; ~duration=30 ms: android.os.strictmode.DiskReadViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1500)
        at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
        at java.io.File.exists(File.java:815)
        at android.app.ContextImpl.getDataDir(ContextImpl.java:2237)
        at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:550)
        at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:747)
        at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:400)
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:174)
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:174)
        at android.support.v7.preference.PreferenceManager.getSharedPreferences(PreferenceManager.java:330)
gregkorossy commented 6 years ago

This is the expected behavior as the preferences are saved to the SharedPreferences which in turn saves them on the device's internal memory, thus the read happening from the disk. This needs to happen during layout (so on the main thread) otherwise there would be no data to show to the user which would result in flickering artifacts when the data is available on a separate thread. You can basically ignore this "warning" because this is the intended behavior. If you want to overcome this problem, you can try this stackoverflow answer.

tcqq commented 6 years ago

Thanks for reply, I understand.