Just to extend the answer from @Rockney and @k2col the improved code can look like:
@NonNull
public static Spanned fromHtml(@NonNull String html) {
if (CompatUtils.isApiNonLowerThan(VERSION_CODES.N)) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
return Html.fromHtml(html);
}
}
Where the CompatUtils.isApiNonLowerThan
:
public static boolean isApiNonLowerThan(int versionCode) {
return Build.VERSION.SDK_INT >= versionCode;
}
The difference is that there are no extra local variable and the deprecation is only in else
branch. So this will not suppress all method but single branch.
It can help when the Google will decide in some future versions of Android to deprecate even the fromHtml(String source, int flags)
method.