重写Activity的onWindowFocusChanged方法,增加以下代码。注意,一定要判断是否获取了焦点,获取了才去设置。如果不判断就会出现问题,譬如两个Activity都需要隐藏状态栏和导航栏,重写了方法onWindowFocusChanged但是没有判断hasFocus,譬如Activiy A跳转到Actitvty,就会出现Activity A 生效了,Activity B没有生效情况。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
LogUtils.d(TAG,"hasFocus = "+hasFocus);
if(hasFocus){
int uiOption = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(uiOption);
}
}