Accepted answer

i have issues in my app regarding strictmode and added the code snippet that basically disables the strictmodehelper

please fix the networking bug.

which method is prefered ..or are they basically doing the same?

@targetapi and @suppresslint have the same core effect: they suppress the lint error.

the difference is that with @targetapi, you declare, via the parameter, what api level you have addressed in your code, so that the error can pop up again if you later modify the method to try referencing something newer than the api level cited in @targetapi.

for example, suppose that, instead of blocking the strictmode complaints about your networking bug, you were trying to work around the issue of asynctask being serialized on newer versions of android. you have a method like this in your code to opt into the thread pool on newer devices and use the default multithread behavior on older devices:

  static public <t> void executeasynctask(asynctask<t, ?, ?> task,
                                          t... params) {
    if (build.version.sdk_int >= build.version_codes.honeycomb) {
      task.executeonexecutor(asynctask.thread_pool_executor, params);
    else {

having @targetapi(11) means that if lint detects that i am using something newer than my android:minsdkversion, but up to api level 11, lint will not complain. in this case, that works. if, however, i modified this method to reference something that wasn't added until api level 14, then the lint error would appear again, because my @targetapi(11) annotation says that i only fixed the code to work on api level 11 and below above, not api level 14 and below above.

using @suppresslint('newapi'), i would lose the lint error for any api level, regardless of what my code references and what my code is set up to handle.

hence, @targetapi is the preferred annotation, as it allows you to tell the build tools "ok, i fixed this category of problems" in a more fine-grained fashion.

Related Query