Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:39:42+00:00 2026-06-10T00:39:42+00:00

There are related questions, such as How can I pass in 2 parameters to

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:39:43+00:00Added an answer on June 10, 2026 at 12:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've look for this, and there's some related questions, but no one gives me
I know that there are many Delphi database related questions available, but I'm considering
So, I know there are a ton of related questions on SO, but none
There's lots of questions on SO related to this, but the ones I scanned
First off, I'm aware that there are many questions related to this, but none
Related question, but not helpful to me: Why cant you pass MYSQL functions into
There are a lot of related questions here on SO, but they all ask
there are lots of questions out there related to shared preferences and the alternatives.
There has been a lot of talk around iPad-Apps / Approval / Store-related Questions.
I know there are a thousand data import related questions on Stack Overflow, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.