Preferences in a navigation drawer

7

I have an activity with a navigation drawer in which if you click on one of the options you should load the preference fragment. For this I do the following but it does not allow me to load the PreferenceFragment with getSupportFragmentManager() .

getSupportFragmentManager().beginTransaction()
                    .replace(R.id.centro_list_frag,  new Settings.Configuracion()).commit();

In the whole application I use getSupportFragmentManager() so in this case I have to use it too since if I use getFragmentManager() it does not load the correct fragment when clicking on the rest of the navigation drawer options.

The preferences activity and its fragment is as follows:

public class Settings extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(R.id.configuracion, new Configuracion());
    ft.commit();

}
  public static class Configuracion extends PreferenceFragment{

    public Configuracion() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferencia);
    }
}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case android.R.id.home:


            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}
}

How could I load the preference fragment with navigation drawer ?

This is how I charge the options in the navigation drawer:

            private void seleccionarItem(MenuItem itemDrawer) {
    Fragment1 fragmentoGenerico=null;
    Fragment2 fragmentoGenerico2=null;
    switch (itemDrawer.getItemId()) {

        case R.id.item_todos:

            fragmentoGenerico = new Fragment1();
            if (fragmentoGenerico != null) {


                getSupportFragmentManager().beginTransaction().replace(R.id.centro_list_frag, fragmentoGenerico).commit();


            }

            break;
        case R.id.item_ninguno:

            fragmentoGenerico2 = new Fragment2();
            if (fragmentoGenerico2 != null) {


                getSupportFragmentManager().beginTransaction().replace(R.id.centro_list_frag, fragmentoGenerico2).commit();

            }
            break;
}
    
asked by adamista 10.05.2016 в 14:29
source

1 answer

1

If we review the documentation PreferenceFragment extends of Fragment , specifically of android.app.Fragment and not of android.support.v4.app.Fragment for that reason you can not use getSupportFragmentManager () .

This would be the correct and most simplified way to load the PreferenceFragment :

    //Carga PreferenceFragment
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new Configuracion()).commit();

This would be the Activity:

public class PrefActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         //Carga PreferenceFragment
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new Configuracion()).commit();

    }

    public static class Configuracion extends PreferenceFragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Carga preferencias de recurso xml.
            addPreferencesFromResource(R.xml.preferencia);
        }
    }
}

With this you should have no problem loading the PreferenceFragment :

    
answered by 10.05.2016 в 19:33