Issue
I have a very simple preferences setup in which a PreferenceFragment
is added to an Activity
. The Activity
is also an OnSharedPreferenceChangeListener
, since I want to update the summary of a particular preference whenever that preference is updated. Here's the Activity
:
public class PrefsActivity extends Activity implements OnSharedPreferenceChangeListener {
private static final String PREF_KEY = "key goes here";
private PrefsFragment pf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pf = new PrefsFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, pf).commit();
// pf.getPreferenceScreen() throws a NullPointerException here
}
@Override
protected void onPause() {
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
updateSummary();
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals(PREF_KEY)) {
updateSummary();
}
}
private void updateSummary() {
Preference p = pf.getPreferenceScreen().findPreference(PREF_KEY);
p.setSummary("Some string containing the updated value");
}
}
The PreferenceFragment is equally simple:
public class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// At this point getPreferenceScreen() returns correctly
}
}
Here's the thing: Calling getPreferenceScreen()
on the PreferenceFragment
, immediately after it's been instantiated and added to the Activity
in onCreate
of PrefsActivity
, throws a NullPointerException
. It seems that getPreferenceScreen()
starts returning a PreferenceScreen
object in onCreate()
of PrefsFragment
, immediately after the call to addPreferencesFromResource()
returns.
So my question is this: Since getPreferenceScreen()
throws an NPE immediately after the PreferenceFragment
has been added to the Activity
, is the onCreate()
of the PreferenceFragment
called asynchronously/in a different thread? Otherwise I would have expected getPreferenceScreen()
to return normally immediately after getFragmentManager().beginTransaction().replace(android.R.id.content, pf).commit()
.
Solution
is the onCreate() of the PreferenceFragment called asynchronously/in a different thread?
No, it is run synchronously on the main thread. That means it has to wait until the Activity
s onCreate
exits before it gets to run. The FragmentManager
schedules the Fragment
callbacks, but they are not executed until the current callback (onCreate
of the activity) is complete.
You have a few other places to access your PreferenceScreen
. OnStart
is called when the UI is ready, so the Fragment
will be ready. onResume
is called after onStart
and is probably the best place to put something since it is invoked after coming back from a pause, too.
Answered By - iagreen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.