gpt4 book ai didi

java - 应用程序在切换 Activity 时退出沉浸模式

转载 作者:太空狗 更新时间:2023-10-29 15:01:12 24 4
gpt4 key购买 nike

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

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);
}

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

最佳答案

尝试从 onPostCreate 中移除 delayedHide。

关于java - 应用程序在切换 Activity 时退出沉浸模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26522516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com