javaandroidandroid-asynctaskrequestui-thread

Does it make sense to use AsyncTask if onPostExcute is not used?


in a code snippet I am looking at, a private class which extends AsyncTask is used. However the onPostExecute method is not used. Only the doInBackground method is used and in that method there is a POST request made. The question I have is, isn't AsyncTask is for updating the UI after doing a job? I mean does it make sense to use it without onPostExecute?

(I am not allowed to share any part of the code, so sorry if this is vague.)

Thank you.


Solution

  • It depends on the task to be performed. You can perfectly use an AsyncTask without calling onPostExecute, but if it doesn't neither use the onProgressUpdate method, then it would mean an incorrect use of its purpose, which is to execute a task in a background thread, and notify the result on the UI thread, or notify progress updates on the UI thread.

    So, if is not using onPostExecute or onProgressUpdate at all, then probably should be replaced with a Thread.

    Also consider that the AsyncTask class has been deprecated in API 30, therefore may be a good time to refactor such code.