NyP
NyP

Reputation: 519

Handling AsyncTask warning class should be static or leaks might occur

I have created as asynctask to upload data to a server as below. But it shows this warning. This AsyncTask class should be static or leaks might occurs...... I have read and tried the weak reference style but i cant be able to integrate in this code. How can I get rid of this warning in the code below?.

 public void ImageUploadToServerFunction(){
        final String imageName1 = GetImageNameEditText1.trim();
        final String userName = GetUserName.trim();
        final String imageView1 = getStringImage1(bitmap1);
      
        class AsyncTaskUploadClass extends AsyncTask<Void,Void,String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog = ProgressDialog.show(UploadActivity.this,"Your Data is Uploading....",false,false);
            }
            @Override
            protected void onPostExecute(String string1) {
                super.onPostExecute(string1);
                progressDialog.dismiss();
             }
            @Override
            protected String doInBackground(Void... params) {
                ImageProcessClass imageProcessClass = new ImageProcessClass();
                HashMap<String,String> HashMapParams = new HashMap<>();
                HashMapParams.put(ImageName1, imageName1);
                HashMapParams.put(UserName, userName);
                HashMapParams.put(ImagePath1, imageView1);
                return imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
            }
        }
        AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();
        AsyncTaskUploadClassOBJ.execute();
    }

Upvotes: 0

Views: 525

Answers (1)

Darren Finch
Darren Finch

Reputation: 15

If you're executing this in the context of a function, you need to make your AsyncTask anonymous. Something like:

public void ImageUploadToServerFunction(){
    final String imageName1 = GetImageNameEditText1.trim();
    final String userName = GetUserName.trim();
    final String imageView1 = getStringImage1(bitmap1);
  
    new AsyncTask<Void,Void,String>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(UploadActivity.this,"Your Data is Uploading....",false,false);
        }
        @Override
        protected void onPostExecute(String string1) {
            super.onPostExecute(string1);
            progressDialog.dismiss();
         }
        @Override
        protected String doInBackground(Void... params) {
            ImageProcessClass imageProcessClass = new ImageProcessClass();
            HashMap<String,String> HashMapParams = new HashMap<>();
            HashMapParams.put(ImageName1, imageName1);
            HashMapParams.put(UserName, userName);
            HashMapParams.put(ImagePath1, imageView1);
            return imageProcessClass.ImageHttpRequest(ServerUploadPath,HashMapParams);
        }
    }.execute();
}

But I highly recommend not using AsyncTask in Android anymore. It's a very dirty way to do asynchronous operations and it rightfully should be deprecated. A better way to solve this would be with RxJava.

Upvotes: 1

Related Questions