Question

Is there any way to prevent the doInBackground method/thread of an AsyncTask from starting until an AlertDialog which is displayed during the onPreExecute method has been dismissed?

doInBackground is only meant to run when onPreExecute completes, however as far as I can tell, having an open AlertDialog does not stop onPreExecute from completing.

Details: Am on my phone at the moment so can't give code sample, but the overview of the code is that it's an app that requires data that can only be accessed via mobile data due to being on telco network. The preExec of my getData asynctask first checks if wifi is up, and if so displays an alert dialog with Disable Wifi and Cancel options.

Cancel should end the task, and Disable Wifi should, well, disable wifi. doInBg method then checks if mobile data connection is up, and if not goes into a sleep loop unti it is. Once it is up, doInBg then gets the data...

Currently, if wifi is on doInBg starts before the dialog is dismissed, getting trapped in the sleep loop until something causes mobile connection to come up... This leads to potential problems I'd like to avoid.

Was it helpful?

Solution

You can try to launch it somewhere to check if your wifi is up, then use AlertDialogListener to catch the action of buttons in AlertDIalog. then you can start your Async inside AlertDialogListener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top