Search code examples
androidparameter-passinghttpresponsemain-activity

How AsyncTask returns parameters to MainActivity


I am new to android. I have written a asynctask class which takes one string as parameter. Asynctask class has two functions doinbackground and onpostexecute. doinbackground is doing a httppost and if the post is successful it is returning a string "Success" to onpostexecute or pasing "Failed" to onpostexecute.

In Mainactivity I am calling the Asyncclass like below: new MyAsyncTask().execute(xmlFile);

But I need to get the string that doinbackground returns in my mainactivity as based on this status I need to update a database filed. Can anyone help me on this issue.

Hypothetically I want to do the below in MainActivity

////////////////////////

Run the asyncclass by passing a string;;;

if doinbackground returns "Success" update database

else don't update

///////////////////////////

Thanks


Solution

  • You can use interface as a callback to the activity.

    You can check blackbelts answer in the below link

    How do I return a boolean from AsyncTask?

    Or you can make AsyncTask an inner class of activity and get the result in onPostExecute.