I have one activation and several fragments, all fragments have the same toolbar as in the activation. How to change ?

Fragment:

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_log, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { return false; } 

in activit:

  public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.search_menu, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setOnQueryTextListener(searchQueryListener); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.logout: fragmentManager.beginTransaction().add(R.id.container, bFrag).commit(); wallcome.setVisibility(View.INVISIBLE); mRecyclerView.setVisibility(View.INVISIBLE); fab.setVisibility(View.INVISIBLE); return true; default: break; } 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

As I understand it, you want to change the menu in the toolbar, then you can use the function invalidateOptionsMenu (), which allows you to update the menu view. Make some variable boolean CHECK = false, the value of which changes to true when you clicked on logout, and then the onCreateOptionsMenu function will look something like this:

  public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.ваше_меню, menu); MenuItem Item1 = menu.findItem(R.id.какой-то_итем1_в_вашем_меню); MenuItem Item2 = menu.findItem(R.id.какой-то_итем2_в_вашем_меню); if (!CHECK){ //логаут ещё не нажат, вид меню начальный }else{ //на логаут нажали, скрыть нужную кнопку в меню или т.п. } return super.onCreateOptionsMenu(menu); } 

When you clicked on the logout, for example, you start the code for calling a fragment, and after this code add a call to the function invalidateOptionsMenu ();

  • and then how to get it back? If I get into activit again. - Fedia
  • I didn’t think about it, it’s probably better to do as told by the link that YuriySPB dropped: D - YungBlade