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.
Yes you can use deprecated methods as long as the depreciated method exists in the framework. By deprecating a method the platform developers are trying to tell you that either something is wrong with the method or there is already better way for doing the task.
If you are using deprecated method then you must keep track of removed apis whenever you upgrade to newest SDK. If you don't want a change at all then check for the reason behind deprecation. If deprecation is because of performance issues then you might consider upgrading to newest methods.
It is so unimportant that you should stop using it because it has been superseded and may be phased out in the future. A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it since it has been superseded and may cease to exist in the future.
It can still be used, but eventually it will not work with other things because it is no longer being supported. Best practice is to use the requested alternative to any depreciated type.
Never use deprecated fields, methods, or classes in new code. Java provides an @deprecated annotation to indicate the deprecation of specific fields, methods, and classes.
Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead. Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With