0

I'm setting a new android app, and I want to exploit the bottom navigation bar in my phone (previous,home) so that i don't have to create a customized one, I want to know if it is possible to set an onClickEvent on these buttons

EdChum
  • 376,765
  • 198
  • 813
  • 562
  • 1
    Check [this post](https://stackoverflow.com/questions/8881951/detect-home-button-press-in-android) and [this post](https://stackoverflow.com/questions/11182703/check-if-back-key-was-pressed-in-android). – Tamir Abutbul May 08 '19 at 17:13

1 Answers1

0

You have to handle both button separately.

  1. For hardware back button (Previous) you have to override onBackPress method in your Activity class. So, you will get the back press event you can change the behavior of back button if you don't want back event just remove super.onBackPress(). If you want to do this from fragment you just need to add your logic like you can get current fragment instance from FragmentManager like below and call fragment method what ever you want to do on that fragment.
    \\ To get current fragment 
    \\ NOTE: I add back stack name as class name.
    public static Fragment getCurrentFragment(FragmentManager fragmentManager) {
        int count = fragmentManager.getBackStackEntryCount();
        if (count > 0) {
            FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1);
            String tag = backStackEntry.getName();
            return fragmentManager.findFragmentByTag(tag);
        }
        return null;
    }
@Override
    public void onBackPressed() {
        // enter code here
        super.onBackPressed(); // Remove this line if you don't want to go back.
    }

  1. For home button handling you can use this answer or this post.

Please comment me if you have more questions.

Nitish
  • 995
  • 7
  • 17