Muhammed Refaat
Muhammed Refaat

Reputation: 9103

Why deprecation happens?

What's the reason that pushes android development team to deprecate some system methods, I know there are some methods that replaced with another methods which are more handful and helpful but some others not even replaced with another methods or are just replaced with one generalized big method which won't be helpful when trying to deal more specifically with some element, for example some methods from Date class:

Date mDate = new Date();
mDate.getDate();            //Deprecated
mDate.getDay();             //Deprecated
mDate.getHours();           //Deprecated
mDate.getMinutes();         //Deprecated
mDate.getMonth();           //Deprecated
mDate.getSeconds();         //Deprecated
mDate.getYear();            //Deprecated
mDate.getTimezoneOffset();  //Deprecated

and the only one is exist now is :

mDate.getTime(); // gives the whole Time & Date as a `long` Value in milliseconds

now, when I get a Date Object and try to get the Year , I have to:

  1. Declare a new Calendar Object.
  2. makes it equal an instance of Calendar Class.
  3. sets this calendar object time to the date I got.
  4. extract the required attributes from this calendar object.

and what if that Data Object contains only the Date with the time equals to zero. now I have to make a workaround to save the time of the calendar before applying date to it, then reset the saved time to it again after applying date to it.

another example found here which illustrates that the entire PictureListener interface is deprecated and There's no sign of what, if anything, is supposed to replace it.

third example when want to get screen width & height, that was direct:

int windowWidth, windowHeight;
windowWidth = getWindowManager().getDefaultDisplay().getWidth(); // Deprecated
windowHeight = getWindowManager().getDefaultDisplay().getHeight(); // Deprecated

but now:

int windowWidth, windowHeight;
DisplayMetrics metrices = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(metrices);
windowWidth = metrices.widthPixels;
windowHeight = metrices.heightPixels;

Upvotes: 1

Views: 157

Answers (2)

clemp6r
clemp6r

Reputation: 3723

Depecration is way to inform developers that there is a better way to do things, and that they should not use the old way anymore.

Framework devs could delete obsolete APIs, but as they don't want to disturb developpers that still uses these APIs, they deprecate them instead. Instead of blaming them, you should thank them for managing changes this way.

Upvotes: 1

mbukowicz
mbukowicz

Reputation: 41

The methods of java.util.Date you've mentioned were not deprecated by Android team, but were deprecated by Sun in Java 1.1. This is just an example of Java legacy. If you think Calendar API is unbearable I would consider using Joda Time instead.

Upvotes: 4

Related Questions