0

In my Application, i'm transitioning to a DetailsFragment when the user clicks on a list item. and there are TWO options to be back at the main Fragment (the List Fragment).

  • Press the back button. (no problem here, because I handle this in onBackPressed() in MainActivity)
  • Press the Toolbar back arrow (Here is my problem).

When the user presses the toolbar back arrow, I call the following

getActivity().getSupportFragmentManager().popBackStack();

how can I intercept this event in MainActivity? (There are some manipulations I am doing in MainActivity when the List Fragment is visible to the user.

Gopal
  • 1,734
  • 1
  • 14
  • 34
Steve NDENDE
  • 19
  • 1
  • 9
  • It require to Overwrite `onOptionsItemSelected` method to intercept the event of toolbar back arrow . You can find full answer in [how to override action bar back button in android?](http://stackoverflow.com/questions/14437745/how-to-override-action-bar-back-button-in-android) – Al-Mustafa Azhari Jan 23 '17 at 11:35

1 Answers1

0

Just put this code

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
Akash
  • 961
  • 6
  • 15
  • I used the following in my DetailsFragment `@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { getActivity().onBackPressed(); } return true; }` – Steve NDENDE Jan 23 '17 at 12:41
  • it's okay now, but this applies only to top back arrow – Steve NDENDE Mar 06 '17 at 06:34