我制作的应用程序有多个活动,我希望它完全处于沉浸模式。但是每当我在活动之间切换时,导航栏和操作栏会出现一秒钟,然后应用程序就会回到沉浸模式。我希望应用程序永远不会退出沉浸模式,并且在切换活动时导航栏不会弹出。下面是我编写的代码:
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);
}
我在所有的活动中都使用了这行代码。每次我切换活动时,导航栏和操作栏都会弹出,我该如何解决这个问题?