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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With