Search code examples
androidkotlinandroid-preferencesandroid-strictmode

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


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)

Solution

  • SharedPreferences are read from the disk the first time they are accessed and then cached in memory, hence your DiskReadViolation in strict mode.

    While running all I/O as non-blocking off the main thread is ideal, even if you enable strict mode on Google Apps you will see red flashes at app start-up so you may not easily be able to get around your strict-mode violation.

    The main thing for you to be concerned about is long-running HTTP requests or long-running database operations running on the main thread - these are to be avoided and the solutions involve using something like AsyncTask, RxJava or Kotlin coroutines.

    For now, the SharedPreference and the associated setPreferenceFromResource have a modes of operation that you do not control and so you are unlikely to make things better by adding asynchronous code around these.