应用程序在切换活动时退出沉浸式模式

时间:2014-10-23 06:38:37

标签: java android android-fullscreen

我制作的应用程序有多项活动,我希望它完全处于沉浸式模式。但每当我在活动之间切换时,导航栏和操作栏会出现一秒钟,然后应用程序返回沉浸式模式。我希望它能让应用程序永远不会退出沉浸式模式,当我切换活动时导航栏不会弹出。这是我写的代码:

private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

private View mDecorView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_home);

    backBtn=(Button)findViewById(R.id.backBtn);
    homeBtn=(Button)findViewById(R.id.homeBtn);
    settingsBtn=(Button)findViewById(R.id.settingsBtn);
    settingsBtn.setOnClickListener(navBtnListener);

    if(getIntent().getExtras()!=null){
        serverIP=getIntent().getExtras().getString("IP");
    }

    Toast.makeText(this, serverIP, Toast.LENGTH_LONG).show();

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mDecorView = getWindow().getDecorView();
    mDecorView.setOnSystemUiVisibilityChangeListener(
            new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int flags) {
                    boolean visible = (flags & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
                    controlsView.setVisibility(visible
                            ?View.VISIBLE
                            :View.GONE);
                }
            });
    contentView.setClickable(true);
    final GestureDetector clickDetector = new GestureDetector(this,
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    boolean visible = (mDecorView.getSystemUiVisibility()
                            & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
                    if (visible) {
                        hideSystemUI();
                    }
                    return true;
                }
            });
    contentView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return clickDetector.onTouchEvent(motionEvent);
        }
    });

    showSystemUI();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(3);
}


@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    // When the window loses focus (e.g. the action overflow is shown),
    // cancel any pending hide action. When the window gains focus,
    // hide the system UI.
    if (hasFocus) {
        delayedHide(AUTO_HIDE_DELAY_MILLIS);
    } else {
        mHideHandler.removeMessages(0);
    }
}

private void hideSystemUI() {
    mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}

private void showSystemUI() {
    mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

private final Handler mHideHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        hideSystemUI();
    }
};

private void delayedHide(int delayMillis) {
    mHideHandler.removeMessages(0);
    mHideHandler.sendEmptyMessageDelayed(0, delayMillis);
}

我在所有活动中都使用了这段代码。如何在每次更改活动时解决导航和操作栏弹出的问题?

1 个答案:

答案 0 :(得分:0)

尝试从onPostCreate中删除delayedHide。