Android Immersive Mode and Backward Compatibility

I want my application to be submerged when appropriate, and to mimic some functions when the api is too low. Is this a good way to do this? 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
            );}
}
+4
source share
1 answer

No, this is the best way to do this (or, well, this is the same system that I use in my application)

Just notice, do apiTooLowForImmersive staticandpublic

public static boolean apiTooLowForImmersive = false;

and assign it a value in the block static.

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.

+2

Source: https://habr.com/ru/post/1536345/


All Articles