Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to have so many deprecated methods in backward-compatible code?

I'm writing an Android application which is targeted to API level 15 but I also want to keep backward-compatibilty with older API levels (min-sdk 7).

I'm going to reach this by putting conditions deciding which code to use according to current API level (as shown below). I suppose this is a good approach, I just want to ask if it's OK to have so many deprecated methods (like display.getWidth()), because there is quite many changes between API level 8 and 15. And if so, if it's a good use of @SuppressWarning("deprecation") in this case?

Wouldn't it be better to use multiple APKs for example for API levels <= 8 and >= 9? (Even though it's not recommended by developer.android.com.

    Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    Point screenSize = new Point();

    // calculate the width
    float width;
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
      display.getSize(screenSize);
      width = screenSize.x;
    } else {
      width = display.getWidth();
    }

Thanks!

like image 412
Marcel Bro Avatar asked Apr 15 '12 18:04

Marcel Bro


1 Answers

You have to ask yourself a few questions:

  • Was it deprecated for the API levels that will be executing this code?
  • If so, is an alternative suggested or available?

In your case, getWidth() is deprecated in favor of using getSize(Point), which requires API level 13. So prior to API level 13, all you have is getWidth(), which at the time was not deprecated. The reason these deprecated methods are kept around is largely with backwards compatibility in mind (along with avoiding breaking everyone's apps which depend on it).

So to answer your questions, yes, this is fine in this case, and yes, it's a good use of @SuppressWarning("deprecation").

like image 73
Jason Robinson Avatar answered Sep 25 '22 08:09

Jason Robinson