Search code examples
androidandroid-asynctaskprimitivevariadic-functions

How can you pass multiple primitive parameters to AsyncTask?


There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as parameters to an AsyncTask, so I want to share what I discovered. This subtlety is not captured in the existing questions and answers, so I want to help out anyone who runs into the same problem as I did and save them the pain.

The question is this: I have multiple primitive parameters (e.g. two longs) that I want to pass to an AsyncTask to be executed in the background--how can it be done? (My answer...after struggling with this for awhile...can be found below.)


Solution

  • It is (strictly-speaking) NOT possible to pass multiple primitives to AsyncTask. For example, if you want to perform myTask.execute(long1, long2) and try to set up private class myTask extends AsyncTask<long, Void, Void> with the corresponding method:

    @Override
    protected LocationItemizedOverlay doInBackground(long... params) {...}
    

    your IDE will likely complain about needing to override a supertype method. Note that you are using the so-called Varargs method signature for doInBackground, where (long... params) is like saying "I accept a variable number of longs, stored as an array called params. I don't completely understand what causes a compiler/IDE complaint to be raised, but I think it has to do with how the generic class Params is defined.

    In any case, it is possible to achieve what you want with no problem, provided you correctly cast your primitives to their respective non-primitive wrappers (e.g. int => Integer, long => Long, etc.). Actually, you don't need to explicitly cast your primitives to non-primitives. Java seems to handle that for you. You just need to set up your ASyncTask as follows (for the example of longs):

    private class MyTask extends AsyncTask<Long, Void, Void> {
    
        @Override
        protected void doInBackground(Long... params) {
            // Do stuff with params, for example:
            long myFirstParam = params[0]
        }
        ...
    }
    

    You can then use this class as you originally intended, e.g.:

    MyTask myTask = new MyTask();
    myTask.execute(long1, long2);
    

    Or for any number of primitives that you would like, PROVIDED THEY ARE OF THE SAME TYPE. If you need to pass multiple types of primitives, this can also be done, but you will need to modify the above to:

    private class MyTask extends AsyncTask<Object, Void, Void> {
    
        @Override
        protected void doInBackground(Object... params) {
            // Do stuff with params, for example:
            long myLongParam = (Long) params[0];
            int myIntParam = (Integer) params[1];
    
        }
        ...
    }
    

    This is more flexible, but it requires explicitly casting the parameters to their respective types. If this flexibility is not needed (i.e. a single data type), I recommend sticking to the first option, as it's slightly more readable.