the continuation of my question is a driver with it starts a fragment, in this fragment there is a foliage when you click on an element of which one more fragment starts to appear in a new window. The code for this fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { myView = inflater.inflate(R.layout.fragment_scale_mineral, container, false); MainActivity.toggle.setDrawerIndicatorEnabled(false); // получаем значение Bundle bundle = getArguments(); if (bundle != null) { strNameMineral = bundle.getString(KEY_MIN); if (strNameMineral != null) { // меняем заголовок тулбара ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(strNameMineral); ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } return myView; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Log.i(LOG_TAG, "нажали кнопку назад"); return true; } return super.onOptionsItemSelected(item); } 

Can't access the button back in the bar.

    2 answers 2

    Perhaps you need to add in onCreateView or OnCrete fragment

     setHasOptionsMenu(true); 

    and override the onCreateOptionsMenu method there

     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); } 
    • one
      did not help, maybe the wrong method of opening a fragment from another fragment? - Maxim Fomichev
    • @ MaksimFomichev, hmm ... And what if it is true return from the onCreateOptionsMenu ? .. - Yuriy SPb
    • one
      @ Maksim Fomichyov, fragments, it seems, look normal ... But MainActivity.toggle does not look very ... Can you change the driver not from a fragment, but from an activit? And in the same place it is simple to check whether there is a running fragment or not, and, depending on this, do something? - Yuriy SPb
    • I read here that this is a problem and it's easier to create stackoverflow.com/questions/9831728/… I will try to try - Maxim Fomichev
    • I didn’t get anything, here’s the last post an interesting solution to stackoverflow.com/questions/13086840/… but it didn’t help me, I’m not caught pressing the home button in any way ((((without a button means ((( Maxim Fomichev

    Actually, here is the solution to all problems, insert this code into the MainActivity

     getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); // show back button toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } else { //show hamburger getSupportActionBar().setDisplayHomeAsUpEnabled(false); toggle.syncState(); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawerLayout.openDrawer(GravityCompat.START); } }); } } }); 

    taken here