isValidFragment Android API 19 isValidFragment Android API 19 android android

isValidFragment Android API 19


Try this... this is how we check validity of fragment.

protected boolean isValidFragment(String fragmentName) {  return StockPreferenceFragment.class.getName().equals(fragmentName);}


Out of pure curiosity, you can also do this as well:

@Overrideprotected boolean isValidFragment(String fragmentName) {    return MyPreferenceFragmentA.class.getName().equals(fragmentName)            || MyPreferenceFragmentB.class.getName().equals(fragmentName)            || // ... Finish with your last fragment.;}


I found I could grab a copy of my fragment names from my header resource as it was loaded:

public class MyActivity extends PreferenceActivity{    private static List<String> fragments = new ArrayList<String>();    @Override    public void onBuildHeaders(List<Header> target)    {        loadHeadersFromResource(R.xml.headers,target);        fragments.clear();        for (Header header : target) {            fragments.add(header.fragment);        }    }...    @Override    protected boolean isValidFragment(String fragmentName)    {        return fragments.contains(fragmentName);    }}

This way I don't need to remember to update a list of fragments buried in the code if I want to update them.

I had hoped to use getHeaders() and the existing list of headers directly, but it seems the activity is destroyed after onBuildHeaders() and recreated before isValidFragment() is called.

This may be because the Nexus 7 I'm testing on doesn't actually do two-pane preference activities. Hence the need for the static list member as well.