@TargetApi code style question


#1

In Listing 6.4, we add @TargetApi(11) to tell the linter to ignore the ActionBar lines. Wouldn’t it be better practice to put the ActionBar code into another method (called from onCreate), annotate that method with TargetApi, and leave onCreate without the TargetApi annotation? What if you or someone else later comes along and adds newer API code to onCreate() forgetting to check the SDK version?

[code]
@TargetApi(11)
private setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setSubtitle(“Bodies of Water”);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, “onCreate() called”);
setContentView(R.layout.activity_quiz);
setupActionBar();
}[/code]