Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass different objects as a parameter to AsycTask?

I am using following code to create an AsyncTask.

public  class SaveFileToExternalStorage extends AsyncTask<File, Void, Boolean>{

    protected Boolean doInBackground(File... file) {

        DalCategories c= new  DalCategories();
        boolean result  = c.saveObject(customlistobject,file[0]);

        return result;    
    }


    protected void onProgressUpdate() {       
        //setProgressPercent(progress[0]); 

    } 

    protected void onPostExecute(boolean result) {     
        //showDialog("Downloaded " + result + " bytes");  

    }

}

Now I want to pass it two parameters customlistobject and File objects with void progress and boolean return type.

I don't know how to pass that customlistobject to my AsyncTask along with the File object.

like image 735
UMAR-MOBITSOLUTIONS Avatar asked Feb 03 '11 11:02

UMAR-MOBITSOLUTIONS


People also ask

Why did AsyncTask get deprecated?

This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

How can I use AsyncTask in different activities in android?

AsyncTask class is firstly executed using execute() method. In the first step AsyncTask is called onPreExecute() then onPreExecute() calls doInBackground() for background processes and then doInBackground() calls onPostExecute() method to update the UI.

What can I use instead of AsyncTask in Java?

Here, we are using the Executor class as an alternative to the deprecated AsyncTask. First, create an instance of the executor by using any of the factory methods: private final Executor executor = Executors.


3 Answers

A not perfect but working solution is to use Object as parameter.

public  class SaveFileToExternalStorage extends AsyncTask<Object, Void, Boolean>{
    protected Boolean doInBackground(Object... param) {
        File file = (File) param[0];
        List list = (CustomList) param[1];
        return result;    
    }

    protected void onProgressUpdate() 
    {       
        //setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(boolean result) 
    {     
        //showDialog("Downloaded " + result + " bytes");  
    }
}

This code is just a sample, you should ensure that the objects at index 0 and 1 are really a File or your CustomList by using instanceof.

Octavian Damiean has also written a good method...

like image 72
WarrenFaith Avatar answered Sep 19 '22 16:09

WarrenFaith


One option is to have a constructor of your AsyncTask that takes those parameters

public  class SaveFileToExternalStorage extends AsyncTask<File, Void, Boolean>{
    List customlistobject;

    public SaveFileToExternalStorage(List aList) {
        customlistobject = aList;
    }

    protected Boolean doInBackground(File... file) {

        DalCategories c= new  DalCategories();
        boolean result  = c.saveObject(customlistobject,file[0]);

The other option is to pass object as first type parameter of AsyncTask:

public  class SaveFileToExternalStorage extends AsyncTask<Object, Void, Boolean>{

    protected Boolean doInBackground(Object... objs) {

        File file = (File) ibjs[1];
        List customlistobject =  (List) objs[2];
        DalCategories c= new  DalCategories();
        boolean result  = c.saveObject(customlistobject,file[0]);

This works, as the actual parameter to doInBackground() is a varargs list and not a single object.

like image 42
Heiko Rupp Avatar answered Sep 20 '22 16:09

Heiko Rupp


If you need to pass it two objects as a parameter just create your own holder and send it the holder objects.

You create an object that holds your custom object along with the File object. Depending on the situation you can also make your custom object just hold the File object and pass the AsyncTask the custom object.

Inside of the doInBackground method just extract the objects so you can process them as necessary.

like image 35
Octavian A. Damiean Avatar answered Sep 20 '22 16:09

Octavian A. Damiean