Loading data for an Activity as soon as application starts

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.

Answers


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:

  1. User starts the application
  2. The Login-screen is shown
  3. User enters name/pass and hits "Login"
  4. AsyncTask to handle login is fired - A ProgressDialog is shown meanwhile.
  5. Login task finishes - the Main Activity is started.
  6. The data loading AsyncTask is fired - A ProgressDialog is shown while data is being loaded
  7. Data loading finishes, ProgressDialog dissappears 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 Service and then having the Main Activity show a ProgressDialog when it starts, if the Service is not done loading yet.


You should not start activity that requires unavailable downloaded data before async task with download completes. For better user experience you should show some kind of splash screen with process dialog (downloading...) to let user know about unfinished background process.


Need Your Help

How to create two or more Unique Columns in SQL Azure?

sql azure unique

I want to select two columns in my table and make them unique but I don't know how to do it in SQL Azure database. As you can see in the image below, it doesn't show any option to modify the table

How to get only photos from user's Facebook newsfeed using graph api?

javascript facebook facebook-graph-api facebook-fql

I combed through the Facebook documentation but didn't see an obvious way to return ONLY photos on a user's newsfeed. Specifically:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.