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;
}