Android Immersive reset mode when activity changes

I am having a problem using immersive mode. Here is the code I put on all the actions:

@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { getWindow().getDecorView().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_IMMERSIVE_STICKY ); } } 

navigationBar and statusBar invisible, that's good.

The problem is that every time I move on to another action, the navigationBar appears and then disappears. I would like the navigationBar display differently.

+5
source share
5 answers

The easiest way to work is to create a base activity class as follows:

 public abstract class BaseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); enableImmersiveMode(); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(hasFocus) { enableImmersiveMode(); } } protected void enableImmersiveMode() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().getDecorView().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_IMMERSIVE_STICKY ); } } } 

And all actions that should work in immersive mode should be inherited from the base class.

I just tested it. This prevents the OSD from hiding and displaying when changing actions.

+4
source

If you ran this short code in the onCreate of activity method, as in the following example:

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); hideSystemUI(); ... } // This snippet hides the system bars. private void hideSystemUI() { // Set the IMMERSIVE flag. // Set the content to appear under the system bars so that the content // doesn't resize when the system bars hide and show. getWindow().getDecorView().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 // hide nav bar | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar | View.SYSTEM_UI_FLAG_IMMERSIVE); } 

And you must call the onWindowFocusChanged method.

Hope this helps you =)

+2
source

maybe late, but right now I ran into the same problem as you and came across this post. To curiosity, I found a solution that worked for me. I manually called the "onWindowFocusChanged" function and passed the "true" parameter. I called this function in OnCreate before "setContentView (R.layout.activity_main);". This performed the function and set the dive mode (full screen mode) before the layout was added, and now I do not see the hidden animation of the navigation and status bars. I hope I helped you.

This is the code:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); onWindowFocusChanged(true); setContentView(R.layout.activity_main); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);} } 
+2
source

The real solution is to call setSystemUiVisibility on onResume and onWindowFocusChanged .

 @Override protected void onResume() { super.onResume(); hide(); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); hide(); } public void hide() { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } 
+1
source

I also ran into this problem. There seems to be no way to prevent Android from displaying the navigation bar again after configuration changes.

To make matters worse, it is also not guaranteed when exactly the system interface will be restored. According to my tests, on some devices, the navigation bar may appear even after onWindowFocusChanged and onResume .

The only reliable way I found to prevent unwanted isInFullScreenMode from appearing is to add the isInFullScreenMode flag and implement View.OnSystemUiVisibilityChangeListener something like this:

 @Override public void onSystemBarsVisible() { if (isInFullScreenMode) { // If Android presented system bars // but our app doesn't need them at this point // just call hideSystemUi() again hideSystemUi(); return; } } @Override public void onSystemBarsHidden() { if (!isInFullScreenMode) { // Similar technique as shown above showSystemUi(); return; } } 

Of course, sometimes in a turn we see how system panels quickly appear and disappear. But at least we can be sure that the state of the user interface of our application will be reliably restored.

Edit: in order to avoid possible confusion (as can be seen from the comments), I will explain a couple of things:

  • onSystemBarsVisible and onSystemBarsHidden are user methods that were defined in my application. You will not find them on Android platforms;
  • The override keywords are used here because these methods were part of the contract (interface);
  • The application in which I used this functionality is deprecated. However, I still remember that the main idea was as follows (fragment in Kotlin):
 fun onSystemUiVisibilityChange(visibility: Int) { if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) { // The system bars are visible onSystemBarsVisible() } else { // The system bars are NOT visible. onSystemBarsHidden() } } 
+1
source

Source: https://habr.com/ru/post/1215824/


All Articles