Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

José Eduardo Eguiguren
José Eduardo Eguiguren
17,555 Points

Best Practice for supporting new API features on older versions of Android

I was wondering if there is a better way to support all the latest features that every major Android version introduces while still having a wide support for older versions. Let's say I want to implement activity transitions, but I don't want Lollipop to be the minimum version required, or support for multi screen on the latest Nougat version, but still have a fallback functionality for prior versions.

Should I use if statements whenever I'm using an SDK specific feature?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    buttons[i].setBackground(getDrawable(tilesList.get(i).getId()));
} else {
    buttons[i].setBackground(getResources().getDrawable(tilesList.get(i).getId()));
}

Or populate methods with annotations such as

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void fancyTransition() {
    getWindow().setEnterTransition(new Slide(Gravity.RIGHT));
}

or

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void fancyTransition() {
    getWindow().setEnterTransition(new Slide(Gravity.RIGHT));
}

I'm curious to know if there is a better way, I want to experiment with new features while still allowing older versions to use my apps with alternate functionality.

Let me know please :)

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

A lot of the time newer features in Android are also added to the Android Support libraries so they can be used in a backward-compatible fashion. For instance with the newer getDrawable you can use ContextCompat.getDrawable() (or DrawableCompat.getDrawable()) instead. While not true all the time (and sometimes may not be the best solution), it's worth checking if there is a "Compat" class that can smooth over the differences in Android versions.

Actually this would be a great idea for a workshop: Dealing with different OS versions.

José Eduardo Eguiguren
José Eduardo Eguiguren
17,555 Points

Although I've used Compat classes before I never really put much attention to them, you're totally right, I'll look them up more often. I agree, that would be a very useful workshop.

Thank you

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

My understanding is that for the time being at least if you target API 14 that should be good for legacy and new versions of Android. :)