Inner PreferenceScreen does not open with PreferenceFragmentCompat

前端 未结 7 1517
难免孤独
难免孤独 2020-12-07 21:12

My inner PreferenceScreen of PreferenceFragmentCompat is not showing, or seems to ignore tapping events.

I created MyPreferenceFragment that exten

7条回答
  •  醉酒成梦
    2020-12-07 21:35

    Solution is to start another fragment of the same class but with different root key. No Activity actions involved.

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey){
        if(getArguments() != null){
            String key = getArguments().getString("rootKey");
            setPreferencesFromResource(R.xml.preferences, key);
        }else{
            setPreferencesFromResource(R.xml.preferences, rootKey);
        }
    }
    
    @Override
    public void onNavigateToScreen(PreferenceScreen preferenceScreen){
        ApplicationPreferencesFragment applicationPreferencesFragment = new ApplicationPreferencesFragment();
        Bundle args = new Bundle();
        args.putString("rootKey", preferenceScreen.getKey());
        applicationPreferencesFragment.setArguments(args);
        getFragmentManager()
                .beginTransaction()
                .replace(getId(), applicationPreferencesFragment)
                .addToBackStack(null)
                .commit();
    }
    

提交回复
热议问题