Max Koretskyi
Max Koretskyi

Reputation: 105449

Action bar is hidden and that shown instantly after that

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

Answers (1)

Hareshkumar Chhelana
Hareshkumar Chhelana

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

Related Questions