How can I begin loading data for my activity as soon as my application starts? At the moment, I have it set up to download information needed as soon the the main screen is launched – which is where the user must login. Depending on the connection speed and how quickly the user logs in, the data may not be loaded and causes issues when the activity is launched, because the data hasn’t been loaded. (The spinner won’t have any data, etc.)
I currently have two AsyncTask – one for the login, and one to download the data. Once the user leaves the screen, it seems as though the AsyncTask stops. I tried putting a timer to wait until a boolean variable is set to true from the task which downloads the data, but that seems to force close the application. Here is an example:
while (!settingsLoaded) {
SystemClock.sleep(1000);
}
When the AsyncTask that downloads the data hits the onPostExecute, I set settingsLoaded = true. Should I be using something else to download the data?
Note — when the application is launched, I do task.execute(); for the application data AsyncTask, and the other AsyncTask which logs in the users is set off dependent on whenever the users hits the ‘login’ button.
Now, you don’t mention what kind of data is being loaded – but from your description of scenario I would suggest you do something in the region of:
ProgressDialogis shown meanwhile.ProgressDialogis shown while data is being loadedProgressDialogdissappears and the user can start using the app.What most likely happens in your scenario is that the Login task finishes before the Data loading task. The way you do it at the moment you have no way of telling which task finishes first.
If you want to start data loading right away, you could do it using a
Serviceand then having the Main Activity show aProgressDialogwhen it starts, if theServiceis not done loading yet.