Skip to content

Instantly share code, notes, and snippets.

@rharter
Last active December 16, 2015 02:58
Show Gist options
  • Save rharter/5366076 to your computer and use it in GitHub Desktop.
Save rharter/5366076 to your computer and use it in GitHub Desktop.
The issue here is that the View.setBackgroundDrawable method has been deprecated but the replacement method, View.setBackground, just delegates to the deprecated method. How can I handle this in a safe manner?
/**
* This won't work because it just relies on the deprecated method.
*/
@Override
public void setDrawable(Drawable drawable) {
setBackgroundDrawable(drawable);
}
@Override
public void setBackgroundDrawable(Drawable drawable) {
if (drawable != null) {
drawable = new DrawableWrapper(drawable);
}
super.setBackgroundDrawable(drawable);
}
/**
* This won't work because super.setDrawable() just delegates to
* setBackgroundDrawable(), which duplicates things.
*/
@Override
public void setDrawable(Drawable drawable) {
if (drawable != null) {
drawable = new DrawableWrapper(drawable);
}
super.setDrawable(drawable);
}
@Override
public void setBackgroundDrawable(Drawable drawable) {
if (drawable != null) {
drawable = new DrawableWrapper(drawable);
}
super.setBackgroundDrawable(drawable);
}
/**
* Unless I'm mistaken, this should never happen. If you deprecate a method
* to replace it with another, shouldn't the actual functionality be moved
* to the new method and the deprecated method be made to delegate to the
* replacement method?
*/
public void setBackground(Drawable background) {
//noinspection deprecation
setBackgroundDrawable(background);
}
/**
* @deprecated use {@link #setBackground(Drawable)} instead
*/
@Deprecated
public void setBackgroundDrawable(Drawable background) {
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment