Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any use case example where @RequiresApi is more useful than @TargetApi

Tags:

android

to suppress API level warning, I usually prefer to use @RequiresApi, than @TargetApi.

As, @RequresApi seems newer and better than @TargetApi, according to RequiresApi vs TargetApi android annotations

But, is there any real use case, where we can solve using @RequiresApi, but not using @TargetApi?

like image 560
Cheok Yan Cheng Avatar asked Nov 08 '22 21:11

Cheok Yan Cheng


1 Answers

@RequiresApi is more clearly, as described in its doc.


Moreover, @RequiresApi has FILED Target:

@Retention(CLASS)
@Target({TYPE,METHOD,CONSTRUCTOR,FIELD})
public @interface RequiresApi {
...

while @TargetApi not:

@Target({TYPE, METHOD, CONSTRUCTOR})
@Retention(RetentionPolicy.CLASS)
public @interface TargetApi {
...

So, @RequiresApi can be used like:

@RequiresApi(api = xxx)
private Foo bar;
like image 181
zwh Avatar answered Nov 15 '22 12:11

zwh