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?
getCurrentHour()
until years pass and API 23 becomes my new minSdkVersion
? getCurrentHour()
or getHour()
based on the result? Thank you for helping me learn.
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:
Sometimes, the deprecated stuff is still usable years later. AbsoluteLayout
was deprecated in API Level 3, back in 2009. It still works as craptastically today as it did back then.
Sometimes, the deprecated stuff will have reduced functionality. Various methods on ActivityManager
are deprecated and return a subset of what they used to, for security and privacy reasons.
Sometimes, the deprecated stuff will be removed outright (see: HttpClient in Android 6.0 (or, actually, don't see it, as it was removed)). This is fairly unusual.
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