Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with deprecated methods in android


I'm currently building an app targeting API 23, with a minimum API of 19.
In API 23 some of the methods of the android.widget.TimePicker component was replaced.

For example:

TimePicker.getCurrentHour();

was replaced by:

TimePicker.getHour();

Now, when using TimePicker in my app I should check whether the device is using API 22 or above with the following if statement:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        TimePicker.getHour();
    else
        TimePicker.getCurrentHour();

What I did was extending the TimePicker class and implementing the deprecated methods like this:

public class TimePicker extends android.widget.TimePicker {

    public TimePicker(Context context) {
        super(context);
    }

    public void setCurrentHour(int hour) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            super.setHour(hour);
        else
            super.setCurrentHour(hour);
    }

    public void setCurrentMinute(int minute) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            super.setMinute(minute);
        else
            super.setCurrentMinute(minute);
    }

    public int getCurrentHour() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            return super.getHour();
        else
            return super.getCurrentHour();
    }

    public int getCurrentMinute() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            return super.getMinute();
        else
            return super.getCurrentMinute();
    }
}

so the user who uses this class won't affect the change of the methods (he should only replace the import of the TimePicker class in his implementation).

Is it the right way doing so? or there is a better solution?

Thanks

like image 425
Moshik L Avatar asked Oct 14 '15 09:10

Moshik L


People also ask

Can you still use deprecated methods in 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.

Is it OK to use deprecated methods?

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.

What happens when a method is deprecated?

Similarly, when a class or method is deprecated, it means that the class or method is no longer considered important. It is so unimportant, in fact, that it should no longer be used at all, as it might well cease to exist in the future.

Is it OK to use deprecated packages?

It means that it is bad practice to use it. 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.


1 Answers

The way you performed is good practice as far as I can read from the shown part.

However, as I have seen so far, the best practice has been to make different subdivisions from each class you intend to publish, and stack the program during installation.

This basically means that if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) goes at the top of the class.

If your project intends to go under more versions, I suggest this:

public class Example extends moreExamples implements additionalExamples{
   switch(Build.VERSION.SDK_INT){
      case Build.VERSION_CODES.M:
         codeVersionM();
         break;
      case Build.VERSION_CODES.L:
         codeVersionL();
         break;
      case Build.VERSION_CODES.K:
         codeVersionK();
         break;
      default:
         errorNoBuildImplemented();
   }
}
like image 142
Bonatti Avatar answered Oct 04 '22 05:10

Bonatti