androiddeprecatedandroid-timepickerandroid-versiondeprecation-warning

How should I be using/not using deprecated methods in Android


I have gone too long without asking this question. I have come across many methods in Android which are deprecated, but still work in newer versions of the API. So what is the risk of using a deprecated method as long as it works?

Here's a more specific question. I'm working with TimePickers and the getCurrentHour() method is deprecated in API 23 and replaced with getHour(). I obviously cannot use getHour() exclusively, since most devices are not yet on API 23, but is using getCurrentHour() "wrong" for the newest version of Android? Which of the following should I do?

  1. Continue using getCurrentHour() until years pass and API 23 becomes my new minSdkVersion?
  2. In code, explicitly check the API version and call either getCurrentHour() or getHour() based on the result?

Thank you for helping me learn.


Solution

  • is using getCurrentHour() "wrong" for the newest version of Android?

    If by "wrong" you mean that it will kill a puppy, no, it will not be wrong. And for API Level 23, it is unlikely to cause any sort of problem.

    In general, "deprecated" means "we have something else that we would like you to use". What happens with deprecated stuff varies:

    Hence, a deprecation warning is a sign to pay attention and ponder what your long-term strategy will be for this particular bit of functionality.

    Which of the following should I do?

    For something as trivial as this method rename, you're probably safe with your first option (stick with the deprecated method until your minSdkVersion rises high enough).

    For anything much bigger than that, though, I'd go with your second option. Sometimes, you will do that version check explicitly. Sometimes, you can use a library that will hide the version check for you. For example, pretty much anywhere in the support-v4 library that you see a ...Compat class (e.g., NotificationCompat, ContextCompat, ActivityCompat), the role of that class is to supply an API equivalent to the latest-and-greatest API level, but one that can be used on a wide range of devices, because it gracefully degrades on older devices by doing those version checks for you.

    Note: no actual puppies were harmed in the creation of this answer