I have a button inside my fragment that I want it to call another fragment that is over the current when it is clicked. follows the fragment code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.layout_fragment_minhaconta, null);
Button botao = (Button) view.findViewById(R.id.editarMinhasInformacoes);
botao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Aqui, deve-se mudar para o outro fragment
}
});
return (view);
}
In the class that calls this fragment, it has been declared so that a navigationDrawer changes the fragments within the same activity:
ActionBarDrawerToggle toggle;
FragmentManager fm = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
// Criando o menu
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
// Fragments
if (savedInstanceState == null) {
FragmentInicio fragInicio = new FragmentInicio();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.layoutFundo, fragInicio, "fragInicio");
ft.commit();
}
}
public boolean onNavigationItemSelected(MenuItem item) {
FragmentMinhaConta fragMinhaConta = new FragmentMinhaConta();
FragmentInicio fragInicio = new FragmentInicio();
FragmentFavoritos fragFavoritos = new FragmentFavoritos();
FragmentCompras fragCompras = new FragmentCompras();
FragmentConfiguracoes fragConfiguracoes = new FragmentConfiguracoes();
FragmentSobre fragSobre = new FragmentSobre();
FragmentTransaction ft = fm.beginTransaction();
switch (item.getItemId()) {
case R.id.nav_menu1:
ft.replace(R.id.layoutFundo, fragMinhaConta, "fragMinhaConta");
ft.addToBackStack("pilha");
break;
case R.id.nav_menu2:
ft.replace(R.id.layoutFundo, fragInicio, "fragInicio");
ft.addToBackStack("pilha");
break;
case R.id.nav_menu3:
ft.replace(R.id.layoutFundo, fragFavoritos, "fragFavoritos");
ft.addToBackStack("pilha");
break;
case R.id.nav_menu4:
ft.replace(R.id.layoutFundo, fragCompras, "fragCompras");
ft.addToBackStack("pilha");
break;
case R.id.nav_menu5:
ft.replace(R.id.layoutFundo, fragConfiguracoes, "fragConfiguracoes");
ft.addToBackStack("pilha");
break;
case R.id.nav_menu6:
ft.replace(R.id.layoutFundo, fragSobre, "fragSobre");
ft.addToBackStack("pilha");
break;
case R.id.nav_menu7:
finish();
break;
}
ft.commit();
DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawerLayout);
if(dl.isDrawerOpen(GravityCompat.START))
dl.closeDrawer(GravityCompat.START);
return false;
}
What I would like now is that the fragment called in the button's click method (which is inside another fragment) overwrites that fragment itself. How to do this?