4
I have a button inside my Ragment that I want it to call another Ragment that is over the current one when it is clicked. follows the code of Fragment:
@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);
}
Being that in the class that calls this Fragment, it was declared so that a navigationDrawer changes the Fragments within this 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 click method (which is inside another Fragment) overwrite(replace) that very Fragment. How to do this?
View this code and adapt :) http://answall.com/questions/137273/como-mudar-a-activity-dentro-de-um-fragment-com-button/137276#137276 If you notice anything from my answer say
– Francisco Melicias