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.
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.
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.
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.
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...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With