Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting default values of multiple SharedPreferences instances/files
    primarykey
    data
    text
    <p>Suppose that I have an application which saves preferences onto two files, <em>preferences1.xml</em> and <em>preferences2.xml</em>. Then, I can retrieve references to the corresponding objects with the following code:</p> <pre><code>SharedPreferences sharedPrefs1 = getSharedPreferences("preferences1", MODE_PRIVATE); SharedPreferences sharedPrefs2 = getSharedPreferences("preferences2", MODE_PRIVATE); </code></pre> <p>In this way I can manipulate the preferences for both and register listeners for changes on both.</p> <p>I have some doubts about the initialization of those two files, with <em>setDefaultValues</em>:</p> <p><strong>Question 1 - PreferenceFragment context</strong>: I've created a <em>PreferenceActivity</em> with two <em>PreferenceFragments</em> and within the <em>onCreate</em> method of each one I execute the following code (replace X with 1 and 2 for fragment 1 and 2):</p> <pre><code>PreferenceManager pm = getPreferenceManager(); pm.setSharedPreferencesName("preferencesX"); PreferenceManager.setDefaultValues(getActivity(),R.xml.preference_fragmentX, false); </code></pre> <p>I've seen that both fragments correctly set their preferences with their default values when launched.. but, given the fact that I can see only a single <strong>_has_set_default_values.xml</strong> file in the <strong>shared_prefs</strong> directory of the app, how does it understand when properties of <em>preferences1.xml</em> and <em>preferences2.xml</em> have been already set? This file is created as soon as <em>setDefaultValues</em> is called in the first opened <em>PreferenceFragment</em>, but even after that, if I open the second <em>PreferenceFragment</em> it correctly initializes default values. How does it understand that it has not yet initialized <em>preferences2.xml</em> and what is the purpose of <strong>_has_set_default_values.xml</strong> given the fact that it does not contains information about which prefereces files have been initialized?</p> <p><strong>Question 2 - Standard Activity context</strong>: when I start my app, the <em>PreferenceActivity</em> is not the first activity started and the user may never open it, so I'd like to initialize the two preference files with their default values also in the main <em>Activity</em>, how can I do that? For default shared preferences it is easy:</p> <pre><code>PreferenceManager.setDefaultValues(this, R.xml.default_preferences, false); </code></pre> <p>For two preference files how should I do? I can not do something like the following since I can not retrieve an instance of <em>PreferenceManager</em> like in the <em>PreferenceFragment</em> classes:</p> <pre><code>PreferenceManager pm = getPreferenceManager(); // NOT AVAILABLE AND NOT WANTED pm.setSharedPreferencesName("preferences1"); PreferenceManager.setDefaultValues(getActivity(),R.xml.preference_fragment1, false); PreferenceManager pm = getPreferenceManager(); // NOT AVAILABLE AND NOT WANTED pm.setSharedPreferencesName("preferences2"); PreferenceManager.setDefaultValues(getActivity(),R.xml.preference_fragment2, false); </code></pre> <p>Here in the main Activity I do not have the need to change the preference files on which the <em>PreferenceManager</em> will work, since we're not in a <em>PreferenceActivity</em>, I simply want to initialize those two files... any help? I hope I've posted a clear question, even if too long.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload