Reputation: 18130
I'm currently going through the Android training, because I'm trying to hide the navigation bar properly. In the training documentation it states:
You can hide the navigation bar on Android 4.0 and higher using the SYSTEM_UI_FLAG_HIDE_NAVIGATION flag. This snippet hides both the navigation bar and the status bar:
then they go on to provide a code sample.
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
As you can see it says that the SYSTEM_UI_FLAG_FULLSCREEN
flag is only available in 4.1 but they say that this block of code is for 4.1. Wouldn't this cause the application to crash? Should my block of code look more like:
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT == 14 || Build.VERSION.SDK_INT == 15){
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);
} else if (Build.VERSION.SDK_INT >=16) {
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
Upvotes: 1
Views: 1140
Reputation: 1007296
Wouldn't this cause the application to crash?
Not necessarily.
Values like View.SYSTEM_UI_FLAG_FULLSCREEN
are static final int
. Their actual numeric value is put into the APK, not a symbol that gets looked up at runtime. Hence, you won't crash just for having the number there.
What setSystemUiVisibility()
will do when an unknown flag is set, though, could vary. Usually, it will be OK, as the platform usually masks the flags to only the bit range used for the platform's API level, and so what's happening in higher-order bits is irrelevant. However, I have not specifically tried this in the case of setSystemUiVisibility()
. But, if Google's sample shows it running on 4.0, and if you run it on 4.0 without issue, you should be fine.
Upvotes: 1