Search code examples
javaandroiduinavigationbarandroid-api-30

Why does the navigationBars() come back on Fullscreen activity?


I work on Android API 30+, and I want to make a fullscreen app. I disabled the navigationBars() here :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivitySplashscreenBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    mContentView = binding.fullscreenContent;
    mContentView.getWindowInsetsController().hide(WindowInsets.Type.navigationBars());

However, when I click anywhere, the navigationBars() come back.

If I do a second click/touch, it disappears again, and this situation is every time.

Any ideas?


Solution

  • After few hours, I found a solution :

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
       super.onWindowFocusChanged(hasFocus);
       if(hasFocus)
       {
           fullSceen(getWindow().getDecorView());
       }
       else {
           try {
               Object wmgInstance = Class.forName("android.view.WindowManagerGlobal").getMethod("getInstance").invoke(null);
               Field viewsField = Class.forName("android.view.WindowManagerGlobal").getDeclaredField("mViews");
    
               viewsField.setAccessible(true);
               ArrayList<View> views = (ArrayList<View>) viewsField.get(wmgInstance);
               for (View view: views) {
                   fullSceen(view);
    
                   view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                       @Override
                       public void onSystemUiVisibilityChange(int visibility) {
                           fullSceen(view);
                       }
                   });
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
           AppUtils.console(this,TAG, "hasFocus == false");
       }
    }
    private void fullSceen(View view){
       view.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);
    }
    

    Depreciated since Android 30+. For last API, use :

            binding = ActivityBinding.inflate(getLayoutInflater());
            setContentView(binding.getRoot());
            mContentView = binding.fullscreenContent;
            mContentView.getWindowInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
            mContentView.getWindowInsetsController().hide(WindowInsets.Type.navigationBars());
    

    Thanks to @Hong Nguyen