Search code examples
javaandroidnexus-7

Navigation bar appears briefly when switching activites


I am currently writing an Android app with a completely full screen experience (the system bar and navigation bar are not displayed until the user swipes down) for a Nexus 7 tablet.

For some reason, when I switch activities, the navigation bar reappears briefly.

To create the full screen effect, I set to the system UI visibility to predetermined options in each activity's onResume(), register a UI change listener, and register a window focus listener.

UI Options

private View decorView = getWindow().getDecorView();

private 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
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

Setting the System UI Visibilility in onResume()

@Override
protected void onResume() {

    super.onResume();

    decorView.setSystemUiVisibility(uiOptions);

}

Setting the System UI Visibility When the UI Changes

decorView.setOnSystemUiVisibilityChangeListener (
            new View.OnSystemUiVisibilityChangeListener() {

                @Override
                public void onSystemUiVisibilityChange(int pVisibility) {

                    if ((pVisibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                        decorView.setSystemUiVisibility(uiOptions);
                    }

                }

});

Setting the System UI Visibility When the Window Has Focus

@Override
public void onWindowFocusChanged(boolean pHasFocus) {

    super.onWindowFocusChanged(pHasFocus);

    if(pHasFocus) {
        decorView.setSystemUiVisibility(uiOptions);
    }

}

All of the things above are done in each activity of the application to ensure that the navigation bar and status bar are hidden when a keyboard goes away, an activity is resumed, etc.

But, I still have the problem of the navigation bar appearing briefly when I switch between activities.

Any help is greatly appreciated! Thank you.


Solution

  • I managed to solve my problem and eliminate some unnecessary code snippets that I had in my app.

    Making an app full screen -- including hiding the navigation and status bar -- involved quite a few parts:



    Set the app theme in the android manifest to "Theme.NoTitleBar.Fullscreen"
    (Done in AndroidManifest.xml)

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    


    Create global variables for the window decor view and UI options
    (Done on the programming side of all activities)

    private View decorView;
    private int uiOptions;
    

    NOTE: Do not define until onCreate()



    Define decorView and uiOptions and call a function to create a UI change listener in onCreate()
    (Done on the programming side of all activities)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        decorView = getWindow().getDecorView();
    
        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
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    
        createUiChangeListener();
    
    }
    


    Set the system UI visibility in onResume()
    (Done on the programming side of all activities)

    @Override
    protected void onResume() {
    
        super.onResume();
    
        decorView.setSystemUiVisibility(uiOptions);
    
    }
    


    Code for the createUiChangeListener() function called from onCreate()
    (Done on the programming side of all activities)

    private void createUiChangeListener() {
    
        decorView.setOnSystemUiVisibilityChangeListener (
                new View.OnSystemUiVisibilityChangeListener() {
    
                    @Override
                    public void onSystemUiVisibilityChange(int pVisibility) {
    
                        if ((pVisibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            decorView.setSystemUiVisibility(uiOptions);
                        }
    
                    }
    
                });
    
    }