Note that there are some explanatory texts on larger screens.

plurals
  1. POsetting initial summary of the preferences on Android 3.0+
    text
    copied!<p>In the next code there is two <code>findpreference(key)</code>, the first is in <code>onCreate()</code> and it returns null, but the second returns the preference correctly. I need the first to work well for initialize the preference summary. I've spent a lot of time trying different ways, and reading documentation and googling but I haven´t found the way, please help.</p> <pre><code>public class Settings30Activity extends PreferenceActivity implements OnSharedPreferenceChangeListener { protected SettingsFragment settingsFragment; @SuppressLint("NewApi") @TargetApi(11) public static class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); settingsFragment = new SettingsFragment(); //findPreference 1 (returns null): Preference stylePref=settingsFragment.findPreference("pref_style"); stylePref.setSummary(sharedPreferences.getString(key, "")); } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("pref_style")) { //findPreference 2(returns the preference ok): Preference stylePref=settingsFragment.findPreference("pref_style"); stylePref.setSummary(sharedPreferences.getString(key, "")); } } </code></pre> <p>}</p> <p>While debugging, I can see in the variables how the the object settingsFragment/mPreferenceManager is null along the activity life cycle, until <code>onSharedPreferenceChanged()</code> is just invoked then settingsFragment/mPreferenceManager/mSharedPreferences gets sharedPrefereces and <code>findpreference()</code> works well. But I don´t know how to do for it works outside <code>onSharedPreferenceChanged()</code>.</p>
 

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