Skip to content Skip to sidebar Skip to footer

What Is The Purpose Of The Return Statement In Asynctask's Doinbackground()?

This aspect of my login system works just fine if I have the return statement set to 0 or 1, but fails if I use null. This is all adapted from http://256design.com/blog/android-lo

Solution 1:

The purpose is to pass the result of your job (which is executed on a worker thread) to onPostExecute, in order to process the result on the UI thread. This is required if you want to update the user interface in response to a successful job run.

Solution 2:

DoInBackground return value to postExecute method, and passing null does not validate condition:

if(headerCode == 202)
activity.login(id);

Solution 3:

Yes its the value you send to postExecute:

http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result)

protected void onPostExecute(Result result)

Runs on the UI thread after doInBackground(Params...).

The specified result is the value returned by doInBackground(Params...).

This method won't be invoked if the task was cancelled.

Parameters: result The result of the operation computed by doInBackground(Params...).

Solution 4:

protectedclass InitTask extends AsyncTask<Context, Integer, Integer>

In the line above we define our sub-class and the three parameters that will be passed to the callbacks. The callbacks look like this:

doInBackground()

@OverrideprotectedIntegerdoInBackground( Context... params )  {
     returnsuper.doInBackground( params )
}

Anything processed in this method is handled in a sperate thread. Note that the data type of the return value is an Integer and corresponds to the type third parameter in the class definition. This value returned from this method is passed to the onPostExecute() method when this thread completes.

And if you pass null in return then it fails condition on method onPostExecuted()

Post a Comment for "What Is The Purpose Of The Return Statement In Asynctask's Doinbackground()?"