The action bar is hidden and displayed immediately afterwards.

I'm trying to toggle the show / hide action bar on the user by clicking on the action, so I implemented this functionality like this in action:

@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 you click on the activity, the action bar is hidden and then immediately shown again. I added an entry and it seems like this method is being run twice, why?

+2


source to share


1 answer


I think dispatchTouchEvent can be called twice when touching down and up, so take one boolean flag and check that flag value before displaying the 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;
}

      

+4


source







All Articles