Before I’m lampooned, for blocking the main UI thread, here is the scenario:
- A Service runs once a day – takes around 15 secs to complete. Heavy database transactions
- By coincidence the user starts the app during that time
- The home screen loads data from the database
- But because the database is locked (step 1), this throws an error.
So I need to FREEZE the UI thread till the service is completed. No AsyncTask because the UI will continue to execute and throw an error. I’ve managed to put the “onResume” function to sleep till the service finishes. But during that time I’m unable to show any dialogs telling the user to wait or any progress bar telling the user to sit tight.
If I try
start onResume
if (service is running?)
show progress dialog
while the service is running, thread.sleep
dismiss progress dialog
end onResume
The thread sleeps fine and wakes up great. But the damn progress dialog doesn’t show till onResume…resumes! Bottom line, how do I show an indicator (progressdialog or dialog) before I put the main thread to sleep?
I’m open to solutions that load another activity till the service resumes. I’ve tried that, but the previous activity continues on happily 😐
You can not show a progress dialog if you are blocking the UI thread.
I guess you want to invoke the dialogue in onResume, but the UI won’t start to show the dialog before onResume and onPaused are done. But as you are blocking the UI thread in OnResume this will not be reached until the blocking is over. Therefor its not possible.
You need to put the waiting in an async task and callback to your UI thread when done.
Good would be to take a loading-activity that starts your actual activity when the DB update is finished.