Java – Will using new constants break my Android application?

Will using new constants break my Android application?… here is a solution to the problem.

Will using new constants break my Android application?

I’m checking the orientation of the screen within the View using the following method:

getResources().getConfiguration().orientation

The new version of Android offers more directional states, so I’m thinking about using a switch to handle all the states I want to handle.

switch (getResources().getConfiguration().orientation) {
    case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
    case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
    case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
    case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
}

Now, does this break apps on old devices? Given that some constants are new to API 9+? I’m not sure, because I’m not sure how the compiler handles the final static. Are they inline or are they quoted? I think this method should be fine if they are inlined.

Thanks

Solution

Now, will this break the application on old devices?

Not if you use them your way. The Java compiler will replace symbols with their digital equivalents at compile time.

However, you cannot get these values by doing reflection on older devices, although this would be a rather unusual technique.

Related Problems and Solutions