首页 > 解决方案 > What is the SuppressLint Java annotation?

问题描述

I am trying to understand the purpose of the @SuppressLint Java annotation in the following code:

@SuppressLint("StaticFieldLeak")
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new 
  ProgressDialog(MyActivity.this);
        mProgressDialog.setTitle("Loading Profile");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

What exactly is it used for?

标签: javaandroid

解决方案


Android Studio uses Lint to check your code for common errors. These are not things that would result in a build error, but violations of best practices or indications of other things that are done incorrectly. The @SuppressLint command suppresses these warnings.

In this case, you appear to be using a non-static inner AsyncTask class, which can leak a context, so there would normally be a warning about it. Putting that command in suppresses the warning. It is generally preferable to fix the warning rather than suppress it unless you know it is not a real issue.


推荐阅读