Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain Immersive mode when DialogFragment is Shown

I have an Android Application that is made using Fragments

I am hiding the bars at top and bottom of my screen, using the following code.

 @Override
protected void onResume() {
    super.onResume();
    isInBackground = false;

    if(null == getFragmentManager().findFragmentById(R.id.content_container))
    {
        getFragmentManager().beginTransaction().add(R.id.content_container,new PresenterFragment(), PresenterFragment.FRAG_TAG).commit();
    }

    if(Build.VERSION.SDK_INT >=19)
    {
        View decorView = getWindow().getDecorView();
        int uiOptions = 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_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                View decorView = getWindow().getDecorView();
                int uiOptions = 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_STICKY;
                decorView.setSystemUiVisibility(uiOptions);
            }
        });
    }
}

When the soft keyboard it shown the bars show, I can live with this as they hide when the keyboard is dismissed.However if a dialog Fragment is show while the soft keyboard is shown then when both the keyboard and the dialog fragment are dismissed they bars remain over the top of the application.

My 3 questions are

Is it possible to stop the softkeyboard for changing the UI mode?

Is it possible to stop the showing of DialogsFraments from changing the UI mode?

edit: I used to below code to see if the keyboard is shown

public static boolean isKeyBoardShown(){
    InputMethodManager imm = (InputMethodManager)currentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isAcceptingText()) {
        return true;
    } else {
        return false;
    }
}

-> I know that there is a work around for dialogs in an activity but I can't see one or rework the code to work in a DialogFragment

If neither is possible why does the app get stuck in the wrong UI mode when there is both a shown keyboard and DialogFrament?

like image 448
Mark Gilchrist Avatar asked Sep 24 '15 10:09

Mark Gilchrist


People also ask

What is the difference between dialog & DialogFragment?

DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs. It allows the FragmentManager to manage the state of the dialog and automatically restore the dialog when a configuration change occurs.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

What is DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.


1 Answers

I know this is old but I was struggling with this for a while as I had dialogFragments and a drop down in my navigationView that kept revealing the system UI needlessly. Here are the things I did that ended up working (the first two pieces I found a lot of places):

In my AndroidManifest.xml on the application

android:theme="@style/AppTheme.NoActionBar"

In my activity

View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);

And what ended up being the key for me is in my styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>
like image 200
StevenJ Avatar answered Oct 22 '22 00:10

StevenJ