4

does TargetApi mean that code must be called under a specific version or must be called greater than or equal specific version?for example

TargetApi(23) means that use for (23 and below ) or (23 and above)?

ahmetvefa53
  • 581
  • 6
  • 20
  • 1
    Possible duplicate of [Is @TargetApi annotation just for one Api level or above?](https://stackoverflow.com/questions/36825725/is-targetapi-annotation-just-for-one-api-level-or-above) – bunbun Nov 08 '18 at 01:01

2 Answers2

3

Target Api 23 means that the annotated method should run only on api 23+ devices. You should use it with IF check for api level of the device. If your app's minimum api level is lower than 23 consider providing a method for older devices.

Pavel B.
  • 805
  • 10
  • 13
0

From TargetApi docs:

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

It's used purely for the linter. Instead of using targetSdkVersion specified in your build.gradle, it will use this API.

When should you use this?

Perhaps when you're using a deprecated API, but you don't want the linter to continue warning you. You explicitly tell the linter, I know that this is deprecated but I don't care, I am using it as if I was on an older API level. You might (should) also add a comment nearby.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167