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
Asked
Active
Viewed 21 times
0

EdChum
- 376,765
- 198
- 813
- 562
-
1Check [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 Answers
0
You have to handle both button separately.
- For hardware back button (Previous) you have to
override
onBackPress
method in yourActivity
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 removesuper.onBackPress()
. If you want to do this from fragment you just need to add your logic like you can get current fragment instance fromFragmentManager
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.
}
Please comment me if you have more questions.

Nitish
- 995
- 7
- 17