findFragmentById or findFragmentByTag for PreferenceFragment

931 views Asked by At

I have a PreferenceActivity that add PreferenceFragment via Headers:

public class SettingsActivity extends PreferenceActivity {
...

@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.pref_headers, target);
}
...
}

Now I need to call a method of PreferenceFragment from PreferenceActivity but I can't use Fragment fragment = getFragmentManager.findFragmentByTag("tagfragment"); because using Headers to add PreferenceFragment I don't know the TAG or the ID assigned to the fragment.

How can I solve?

[SOLVED]

Solved using this method: https://stackoverflow.com/a/18005721/570488

1

There are 1 answers

5
Xplouder On

Make sure the PreferenceActivity is already loaded when you call this:

MyPreferenceFragment frag = (MyPreferenceFragment) getFragmentManager().findFragmentById(android.R.id.content);

MyPreferenceFragment should be replaced for your own PreferenceFragment class name.