The method is deprecated android

Understanding deprecated in Android

Google releases new versions of android with a year cycle. In process of which developers write some more useful API, methods, classes which are powerful than existing one. So they encourage developers to use newly written API’s and depricate old methods.

What is deprecation ?

  • In Android deprecation usually means “ We will continue to support this, but we think there are better solutions”.
  • Most of the time features are deprecated rather than immediately removed, to provide backward compatibility, and to give programmers time to bring affected code into compliance with the new standard.

Is it wrong to use Deprecated methods or classes ?

  • The main disadvantage is that the API might not be available in future versions, which will increase the cost of updating your application.
  • If the deprecated method is easy to replace, use the replacement. If it isn’t, it’s up to you to decide whether developing the “future-proof” way is worth the additional effort.
  • setBackgroundDrawable can easily be replaced by setDrawable see in detail here.
  • On the other hand, startManagingCursoris much harder to replace, so one might put off transitioning to ContentProvider until there is no other option.

What happens if i continue using Deprecated methods?

  • Code would continue running as it is until method is removed from SDK. 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.
Читайте также:  Umidigi a5 pro обновление до андроид 10

Conclusion :

By researching why a method is deprecated I often learn interesting things about SDK and different ways of doing same thing. There is often a good reason behind deprecation which leads to better understanding of the Android SDK. So from a learning/growing perspective, it is also a worthwhile effort.

I learned about VibrationEffect class while looking at ‘deprecated’ tag on .vibrate() method. Here is my learning :

Источник

Android Building Backwards Compatible Apps How to handle deprecated API

Example

It is unlikely for a developer to not come across a deprecated API during a development process. A deprecated program element is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers and analyzers (like LINT) warn when a deprecated program element is used or overridden in non-deprecated code.

A deprecated API is usually identified in Android Studio using a strikeout. In the example below, the method .getColor(int id) is deprecated:

If possible, developers are encouraged to use alternative APIs and elements. It is possible to check backwards compatibility of a library by visiting the Android documentation for the library and checking the «Added in API level x» section:

In the case that the API you need to use is not compatible with the Android version that your users are using, you should check for the API level of the user before using that library. For example:

Using this method ensures that your app will remain compatible with new Android versions as well as existing versions.

Easier alternative: Use the Support Library

If the Support Libraries are used, often there are static helper methods to accomplish the same task with less client code. Instead of the if/else block above, just use:

Читайте также:  Как сделать экран вверх ногами андроид

Most deprecated methods that have newer methods with a different signature and many new features that may not have been able to be used on older versions have compatibility helper methods like this. To find others, browse through the support library for classes like ContextCompat , ViewCompat , etc.

Источник

Оцените статью