Toolbar is hidden in nested PreferenceScreen

前端 未结 4 617
一生所求
一生所求 2021-02-02 13:36

I use PreferenceFragment in ActionBarActivity from support-v7 library.
In the Activity I have Toolbar. Everything goes okay, until I o

4条回答
  •  别跟我提以往
    2021-02-02 14:26

    Here comes my solution, which is inspired by the original answer but not that complicated. Maybe it'll help someone...

    layout/settings.xml:

        
    
            
    
            
    
        
    

    Classes:

    public class SettingsActivity extends ActionBarActivity {
      @Override
      protected void onCreate( Bundle savedInstanceState ) {
        setContentView( R.layout.settings );
        super.onCreate( savedInstanceState ); 
        initializeSupportActionBar();
        getFragmentManager().beginTransaction().replace( R.id.content, new MainFragment() ).commit();
      }
    
      @Override
      public void onBackPressed() {
        if( !getFragmentManager().popBackStackImmediate() ) super.onBackPressed();
      }
    }
    
    public class MainFragment extends PreferenceFragment {
    
      public MainFragment() {}
    
      @Override
      public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        addPreferencesFromResource( R.xml.pref_main );
        // "nested" is the `
        findPreference( "nested" ).setOnPreferenceClickListener( new OnPreferenceClickListener() {
          @Override public boolean onPreferenceClick( Preference preference ) {
            getFragmentManager().beginTransaction().replace( R.id.content, new NestedFragment() ).addToBackStack( NestedFragment.class.getSimpleName() ).commit();
            return true;
          }
        } );
      }
    
    public class NestedFragment extends PreferenceFragment {
      ...
    }
    

    I tested it on 4.3 and 5.0.2 and no limitation on nesting levels applies

提交回复
热议问题