I have few fragments extending PreferenceFragmentCompat
class from "preference-v8-28.0.0". In order to inflate the view i use addPreferencesFromResource(..)
as follows:
@Override
public void onCreatePreferences(Bundle bundle, String key)
{
readKeys();
addPreferencesFromResource(R.xml.preference_general);
bindPreferences();
}
When check it with StrictMode enabled as follows:
private void enabledStrictMode()
{
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath()
.build());
}
i have a ThreadMode violation:
2020-08-26 17:44:21.877 17990-17990 D/StrictMode: StrictMode policy violation; ~duration=471 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=327743 violation=2
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1438)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
at java.io.File.exists(File.java:807)
at android.app.ContextImpl.getDataDir(ContextImpl.java:2181)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:504)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:698)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:366)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167)
at android.support.v7.preference.PreferenceManager.getSharedPreferences(PreferenceManager.java:331)
at android.support.v7.preference.Preference.getSharedPreferences(Preference.java:1213)
at android.support.v7.preference.Preference.dispatchSetInitialValue(Preference.java:1561)
at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1293)
at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1305)
at android.support.v7.preference.PreferenceGroup.addPreference(PreferenceGroup.java:260)
at android.support.v7.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:181)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:363)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:170)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:120)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:138)
at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:429)
at org.company.libproject.android.settings.GeneralSettingsFragment.onCreatePreferences(GeneralSettingsFragment.java:106)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:228)
at org.company.libproject.android.settings.BaseSettingsFragment.onCreate(BaseSettingsFragment.java:90)
at org.company.libproject.android.settings.GeneralSettingsFragment.onCreate(GeneralSettingsFragment.java:91)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333)
at android.app.Activity.performStart(Activity.java:6992)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.jav
It's kinda expected that it will read a XML file = IO operation happening on main thread.
Why is it considered as a violation? What's the conceptual difference with any activity inflating it's view in onCreate(..)
with setContentView(..)
[that does not violate it]?
What's the right way to do it non-violating? Should i just ignore it and relax StrictMode check (i'd really like to avoid doing that)?
PS. I know "support library" is deprecated now in favour of "androidx library", but i guess it relates to it too. Anyway i'm looking for a proper solution that works for support library at the moment.