There is a MainActivity with DrawerLayout (created by standard AndroidStudio tools with a template), when you select any of the items, DrawerLayout is created and replicated in the Fragment container, where you can start a new Activity (for example, for detailed information on position). Then the user presses the Back button in the Toolbar and goes not to the fragment from which he came to the second activation, but to the main page. When you press the button back on the phone all the rules. I tried to handle pressing OnMenuItemSelected and implement onBackPressed, but it did not help. Hence the question - what to do, so that when you press the button back in the toolbar, the same action happens as when you press the button back on the phone?

PS: In the logs I see that the fragment is not destroyed until the user presses the button back in the toolbar, it seems that the whole application is restarted.

    1 answer 1

    In the manifest for the new Activity, you must remove the parameter

    android:parentActivityName=".MainActivity" 

    In Activity, you need to process click on the "Back" button.

     @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { finish(); } }