Question

I want my app to be Immersive when appropriate and to mimic some of the functionality when the api is too low. Is this an appropriate way to do that? Is there a more efficient way?

private boolean apiTooLowForImmersive = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT){

        apiTooLowForImmersive = true;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    setContentView(R.layout.activity_menu);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus && !apiTooLowForImmersive ) {
        getWindow().getDecorView()
            .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
            );}
}
Was it helpful?

Solution

No, it's the best way to do it (or well, it's the same system i use in my application)

Just a note, make apiTooLowForImmersive static and public

public static boolean apiTooLowForImmersive = false;

and give a value to it in a static block.

static { 
       apiTooLowForImmersive = 
               (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT); 
}

With this, you can use this field in every class and every time you need to know which code is safe to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top