How do I get return values from execute functions in android?

Viewed 294

In android, is it possible to get return values from functions that are run in execute?

Calling file

Connector connector  = new Connector();
connector.execute("login", ipPop.getText().toString(), username.getText().toString(), password.getText().toString());

Function file

public class Connector extends AsyncTask<String,Void,Void>{
    @Override
    protected Void doInBackground(String... voids) {
        return false;
    }
}
1 Answers

You are using AsyncTask in Android. When your class extends it, you can override 3 more methods. Here you can override onPostExecute which is passed with data from doInBackground return. Since onPostExecute runs on UI thread you can update UI from there like cancelling progressbar.

@Override
protected void onPostExecute(String result) {
   // "result" is data return from doInBackground method
   // execution of result of Long time consuming operation
       
}

Refer this sample for your understanding

As per latest android version, AsyncTask is deprecated avoid using it. Check alternatives

Related