I have an activity that has an empty LinearLayout that serves only to 'host' the fragment. This LinearLayout (called layoutFund) is the FragmentTransaction argument to where one of several fragments created separately is 'hosted'. This happens when the click event of one of the items in a navigationDrawer added to the project is triggered. However, I need help with the following: Within one of these fragments there is a button and I need this button, when clicked, to point to another fragment that is to be hosted in the same 'Background'. How can I perform this procedure?
Here is the code that calls the fragment for the BackgroundFound:
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();
}
}
// Este método cria a instância da fragment e ao ser clicada faz um 'replace' para ela ir atrás da pilha
@Override
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;
}
In case the user clicks on nav_menu1 and the layoutFund hosting the fragmentMyContent will appear inside the fragment a button, and clicking this button (which is inside the fragmentMyContent) I need another fragment (fragmentMyContentUpdate) to be hosted by the BackgroundFound instead hosting the fragmentMy Account. This is in case the user when entering the fragmentMy Account wants to update their registration.
Follow the code in the MyContent fragment with a button click event where you should call the other fragmentMyContentUpdate:
public class FragmentMinhaConta extends 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.btnEditarMinhasInformacoes);
botao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Aqui, deve-se mudar para o outro fragment
mostrarMsg("Mudar", "Aqui deveria mudar de fragment");
}
});
return (view);
}
public void mostrarMsg(String titulo, String mensagem) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(titulo);
builder.setMessage(mensagem);
builder.show();
}
}
Instead of entering the showMsg method, I want it to go into another method that overrides that same fragment by the MyMailContactUpdate. How do I do this?