Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 328
NSouth Avatar asked Sep 27 '15 13:09

NSouth


People also ask

Is it OK to use deprecated methods Android?

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.

What to do with deprecated methods in Android?

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.

Why deprecated method should not be used?

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.

Can deprecated methods still be used?

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.

Can you still use deprecated methods Java?

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.

How do you handle deprecated methods in Java?

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.


1 Answers

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

like image 145
CommonsWare Avatar answered Sep 27 '22 16:09

CommonsWare