Reputation: 105449
I'm trying to toggle show/hide action bar on user click on activity, so I've implemented this functionality like this in activity:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("ACTION BAR", "triggered");
super.dispatchTouchEvent(ev);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
if (actionBar.isShowing()) {
actionBar.hide();
} else {
actionBar.show();
}
return true;
}
However, the problem is that when clicked on activity, the action bar gets hidden but then immediately is shown again. I've added logging and it seems that this method is triggered twice, why so?
Upvotes: 2
Views: 864
Reputation: 24848
I think dispatchTouchEvent might be called two time on touch down and up action so take one boolean flag and check this flag value before showing action bar :
private boolean isManuallyHideShownActionBar;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
ActionBar actionBar = getSupportActionBar();
if(!isManuallyHideShownActionBar){
if (actionBar.isShowing()) {
actionBar.hide();
} else {
actionBar.show();
}
isManuallyHideShownActionBar = true;
}else{
isManuallyHideShownActionBar = false;
}
return true;
}
Upvotes: 4