How do I maintain the Immersive Mode in Dialogs?

后端 未结 7 684
醉酒成梦
醉酒成梦 2020-11-28 01:14

How do I maintain the new Immersive Mode when my activities display a custom Dialog?

I am using the code below to maintain the Immersive Mode in Dialogs, but with th

7条回答
  •  感情败类
    2020-11-28 01:40

    After a lot of research into the issue there is a hacky fix for this, which involved tearing apart the Dialog class to find. The navigation bar is shown when the dialog window is added to the Window Manager even if you set the UI visibility before adding it to the manager. In the Android Immersive example it's commented that:

    // * Uses semi-transparent bars for the nav and status bars
    // * This UI flag will *not* be cleared when the user interacts with the UI.
    // When the user swipes, the bars will temporarily appear for a few seconds and then
    // disappear again.
    

    I believe that's what we're seeing here (that a user-interaction is being triggered when a new, focusable, window view is added to the manager).

    How can we work around this? Make the Dialog non-focusable when we create it (so we don't trigger a user-interaction) and then make it focusable after it's displayed.

    //Here's the magic..
    //Set the dialog to not focusable (makes navigation ignore us adding the window)
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    
    //Show the dialog!
    dialog.show();
    
    //Set the dialog to immersive
    dialog.getWindow().getDecorView().setSystemUiVisibility(
    context.getWindow().getDecorView().getSystemUiVisibility());
    
    //Clear the not focusable flag from the window
    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    

    Clearly this is not ideal but it seems to be an Android bug, they should check if the Window has immersive set.

    I've updated my working test code (forgive the hacky messiness) to Github. I've tested on the Nexus 5 emulator, it will probably blow up with anything less than KitKat but its for proof-of-concept only.

提交回复
热议问题