4

I'm stil new with android dev, so today I stumble upon this code in android docs

Result doInBackground (Params... params)

What is the meaning of Params... ? is it the same as Params? but when using Params (without ...), it throw an error not override abstract.

This confuse me.

whale_steward
  • 2,088
  • 2
  • 25
  • 37

4 Answers4

8

It is a Java concept know as var args. It is sugar syntax to pass array of certain type in as method argument.

So when you say

Result doInBackground (Integer... args)
{
  Integer [] vals=args;
}

and you can access values from that array.

As far as AsyncTask's methods are concerned, you can change argument type from Params to the type you need. Params is just a template which you need to change during implementation.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
2

This in Java is called varargs and is an array of params but with different syntax. you can take a look the doc about this by clicking here

Also this question has an answer for you.

Community
  • 1
  • 1
Robert
  • 1,192
  • 1
  • 10
  • 20
2

AsyncTask is a Class which has three methods. DoInBackground() gets called after onPreExecute(). In some cases, the onPreExecute might be returning some value. This value can be captured by doInBackground() with the Params field. This params canbe either Int, Strings, Collection,Context.

NOTE : Similarly with onPOstExecute(Params params). This params is the value returned in doInBackground.

If you dont have any value to return, then you dont need to use the params values in these methods.

2

Params... here refers to parameters. The ... is a way of representing an array.

Result doInBackground (Params... params)

Here this means that params is an array of Params type. By default the value the function doInBackground receives is stored in params[0]. Sometimes the function might receive another value while executing, that will be stored in higher indexes.

Hope that solves your question :-)

Abhinav Upadhyay
  • 140
  • 1
  • 14