Search code examples
androidandroid-5.0-lollipoptabletandroid-optionsmenu

openOptionsMenu doesn't work on Lollipop Tablet with minSdkVersion 16


I have searched Stack Overflow but there is no link for exact same issue.

The app I am working on is a legacy app. This app has to open options menu on tap of an image icon. The app does not use action bar. This feature was working fine, across devices, until I raised it's minSdkVersion to 16 form 8. Since I raised minSdkVersion to 16 the options menu does not open on Lollipop Tablets. It works fine on Lollipop Smartphone though.

What is the issue here? Is there any workaround?


Solution

  • Always have a targetSdkVersion defined for your project. This tells Android "hey, I was thinking of this API level when I wrote this code". Android will use that for selective bits of forwards compatibility, changing runtime behavior on newer devices to be a little bit closer to what you were thinking of back then.

    Tablets never really used the options menu; starting with Android 3.0, the former options menu was migrated to the action bar. In the absence of targetSdkVersion, apparently Android will treat the minSdkVersion as also being the targetSdkVersion. And, with a minSdkVersion/targetSdkVersion of 16, you are firmly into the action bar world.

    Setting your targetSdkVersion to 8 should revert whatever changes were being applied by your minSdkVersion jumping to 16.

    However, eventually something is going to force your hand to raise your targetSdkVersion to a higher value. Do not assume that you will be able to stick with a targetSdkVersion of 8 forever. Either:

    • Migrate your users to the action bar, or
    • Implement your own options menu lookalike, or
    • Plan to replace the app in its entirety with a modern implementation, or
    • Plan to abandon the app