简体   繁体   中英

Android Activity/PreferenceFragment lifecycle?

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() .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM