Android. Hiding navigation bar sometimes fails

泄露秘密 提交于 2019-12-11 11:08:31

问题


The code below intended to toggle navigation bar visibility every time user clicks on screen. And it works fine except one scenario - very fast tap on screen. In some cases, when user clicks really fast, navigation bar stays on screen despite onClick event triggered.

Here is the code of MainActivity:

package com.example.ui_test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

private final String DEBUG_TAG = "UI TEST";
int uiOptions = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout r = (RelativeLayout) findViewById(R.id.main_layout);
    r.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            toggleUiVisibility();
        }
    });

    View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            Log.d(DEBUG_TAG, "Visibility changed. New flags: " + Integer.toString(visibility, 2));
            uiOptions = visibility;
        }
    });
}

protected void toggleUiVisibility() {
    uiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;        
    getWindow().getDecorView().setSystemUiVisibility(uiOptions);
    Log.d(DEBUG_TAG, "Set visibility to: " + Integer.toString(uiOptions, 2));
};

}

I've tried with different layouts (no views, TextView, fullscreen ImageView), there is no difference in behavior. This is LogCat output, look at 6 line. No onSystemUiVisibilityChange event logged after setSystemUiVisibility() call:

09-21 18:24:19.871: D/UI TEST(3901): Visibility changed. New flags: 0
09-21 18:24:21.139: D/UI TEST(3901): Set visibility to: 10
09-21 18:24:21.171: D/UI TEST(3901): Visibility changed. New flags: 10
09-21 18:24:23.478: D/UI TEST(3901): Visibility changed. New flags: 0
09-21 18:24:24.321: D/UI TEST(3901): Set visibility to: 10
09-21 18:24:25.245: D/UI TEST(3901): Set visibility to: 0
09-21 18:24:25.927: D/UI TEST(3901): Set visibility to: 10
09-21 18:24:25.959: D/UI TEST(3901): Visibility changed. New flags: 10
09-21 18:24:33.625: D/UI TEST(3901): Visibility changed. New flags: 0
09-21 18:24:35.458: D/UI TEST(3901): Set visibility to: 10
09-21 18:24:35.490: D/UI TEST(3901): Visibility changed. New flags: 10
09-21 18:24:36.607: D/UI TEST(3901): Visibility changed. New flags: 0
09-21 18:24:38.294: D/UI TEST(3901): Set visibility to: 10
09-21 18:24:38.326: D/UI TEST(3901): Visibility changed. New flags: 10

I'm using Android 5.1.1 device and 22 target/minimum API level.


回答1:


Ok, it turns out it's a feature, not a bug. And the problem was into the interval between setSystemUiVisibility consecutive calls not in fast tap on screen. I had to dig into Android sources and I found this:

...

// We don't allow the system's nav bar to be hidden
// again for 1 second, to prevent applications from
// spamming us and keeping it from being shown.
 newVal = mForceClearedSystemUiFlags |
          View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
 if (mForceClearedSystemUiFlags != newVal) {
     mForceClearedSystemUiFlags = newVal;
     changed = true;
     mHandler.postDelayed(mClearHideNavigationFlag, 1000);
 }

...

It's HideNavInputEventReceiver in PhoneWindowManager.java. I've also reproduced this "bug" in Album application on Xperia Z2.



来源:https://stackoverflow.com/questions/32699348/android-hiding-navigation-bar-sometimes-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!