0

Immersive(-sticky) mode cannot hide navigation bar completely. When I tap and show popup menu, the navigation bar (with transparent background) is raised like a zombie. This phenomena is same both on API-29 or earlier and on API-30.

Is this API's bug or my code's failure?

Here is a sample code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.inflateMenu(R.menu.main);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (hasFocus) {
            hideSystemUI();
        }
    }

    private void hideSystemUI() {
        Window window = getWindow();
        View decorView = window.getDecorView();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            WindowInsetsController windowInsetsController = decorView.getWindowInsetsController();
            windowInsetsController.setSystemBarsBehavior(
                    WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            );
            windowInsetsController.hide(
                    WindowInsets.Type.statusBars()
                  | WindowInsets.Type.navigationBars()
            );

            window.setDecorFitsSystemWindows(false);
        } else {
            window.addFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
                  | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            );
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                  | View.SYSTEM_UI_FLAG_FULLSCREEN
                  | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                  | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            );
        }
    }
}

layout:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/Theme.AppCompat.Light"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

screenshot

I've already read an old similar question but it has no answer until now.

hata
  • 11,633
  • 6
  • 46
  • 69

2 Answers2

1

I posted a similar comment in another topic (https://stackoverflow.com/a/73372588/14202588) but the current topic is more appropriate.

I may add a remark: The behaviour depends on the focusable status of the popup. If it is false, the navigation bar remains hidden. It's still possible to click in the popup. The main difference that I found between a focusable and a non-focusable popup is the behaviour when we click outside.

When focusable, the popup loses the focus, disappears, and no click event is triggered outside when we click outside.

When non-focusable, the popup doesn't disappear automatically, and the click outside is triggered if setOutsideTouchable(True).

So, to keep hidden the navigation bar, it seems that the popup should be non-focusable. Changing this property may bring a lot of modifications in the code. We have to manage the click events in the background window if a popup is displayed...

Maybe somebody knows how to work simply with API 29+, popups and immersive mode...?

...

The following post https://stackoverflow.com/a/42645333/14202588 gave me an idea if there is no other simpler solution. A transparent background in the popup. Maybe the author spoke about the main window.

If we add a transparent background above the full background window before opening the non-focusable popup, that allows, when clicking out of the popup, to put the focus into the unique transparent background instead of the background window. The click event of the transparent background can be used to dismiss the popup.

  • This might work for a popup window (`Activity`), but a popup menu (`PopupMenu`) has no `focusable` or `outsideTouchable` property. – arlomedia Jan 05 '23 at 00:33
0

I concluded this behavior is intended.

Hiding the navigation bar is limited when the main window is focused. When the popup is activated, like dialogues, it has focus while the main window loses. So the navigation bar reappears.

hata
  • 11,633
  • 6
  • 46
  • 69