Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is @TargetApi annotation just for one Api level or above?

Tags:

android

I am using @TargetApi(23) in my app.

@TargetApi(23)     @Override     public void onAttach(Context context) {         super.onAttach(context);         onAttachToContext(context);     }       @SuppressWarnings("deprecation")     @Override     public void onAttach(Activity activity) {         super.onAttach(activity);         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {             onAttachToContext(activity);         }     }      protected void onAttachToContext(Context context) {      } 

But i can not understand something: @TargetApi(23) annotation's mean "just for Api level 23" or "for Api level 23 and above" ? For example if Api level of device 24, is onAttach(Context context) method called?

like image 344
cimenmus Avatar asked Apr 24 '16 16:04

cimenmus


People also ask

What is TargetApi?

implements Annotation. android.annotation.TargetApi. Indicates that Lint should treat this type as targeting a given API level, no matter what the project target is.

What is Target API in android?

Each app specifies a targetSdkVersion (also known as the target API level) in the manifest file. The target API level indicates how your app is meant to run on different Android versions.

What is Tools TargetApi?

By adding tools:targetApi="m" to an element you tell the lint that the element won't be used on API level below 23 (M). See the attribute documentation. This tells the tools that you believe this element (and any children) will be used only on the specified API level or higher.


1 Answers

TargetApi annotation is just for lint tool purposes and has no outcome in runtime. If you use any API methods just available on 23 within your method and don't declare the TargetApi, you will just get some warnings indicating you're using API's not available in your minimum SDK version. It's your responsibility to call this method with coherence being aware of the API level it will be called from.

like image 194
GoRoS Avatar answered Sep 19 '22 02:09

GoRoS