Search code examples
androidandroid-asynctaskonresumeautologin

Logging in Activity, postOnExecute, Resume


I need to implement auto-login process from an activity. Also I need to show ProgressDialog while logging in. I use AsyncTask for login process, because I haven't succeeded to show ProgressDialog in other way, and I execute the mentioned AsyncTask in onCreate. I read that all the code which is executed after doInBackground must be written in onPostExecute, but I have code in onResume. The onResume is launched when AsyncTask hasn't finished its execution yet. Is it possible to launch onResume only after AsyncTask is finished? Or is it possible to execute other functions after AsyncTask is finished (in general)?


Solution

  •  Is it possible to launch onResume only after AsyncTask is finished?
    

    No, you need to follow the Activity Lifecycle. And the AsyncTask is asynchronous so that it can run while your UI can still do other things. What you can do is put the code that is in onResume() that you want to run after the task has finished in your onPostExecute() method. There are other options but without seeing the code you are trying to run, this would be my suggestion.

    Or is it possible to execute other functions after AsyncTask is finished (in general)?

    Yes. You can put that code in another Activity method and call that from onPostExecute()

    You also can use getStatus() method of AsyncTask to run code based on the Status of your AsyncTask