isValidFragment Android API 19

前端 未结 9 1990
你的背包
你的背包 2020-12-01 03:00

When I try my app with Android KitKat I have an error in PreferenceActivity.

Subclasses of PreferenceActivity must override isValidFragment(String) to ve

9条回答
  •  孤街浪徒
    2020-12-01 03:29

    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 fragments = new ArrayList();
    
        @Override
        public void onBuildHeaders(List
    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.

提交回复
热议问题