Reputation: 240
I am currently facing the following problem: whenever my Android application is started, it needs to execute some time consuming initialization code. Without this code all my activities/services within the application won't work correctly.
So far, I have put this initialization code into a SplashScreen activity, which I declared as MAIN activity in the manifest. Once the initialization code has been executed, I finish() the splash screen and start the actual main activity, i.e. an activity consisting of several tabs, from where the user can reach several other activities.
The problem is now the following: when my app is put in the background, after some time and after launching other apps, my app/process is killed. When I re-launch it from the homescreen, Android restores the activity stack (task) and invokes onCreate() on them. However, the splash screen activity and hence the initialization code aren't executed, which results in an exception.
I could put now the initialization code in the application's onCreate(), however this results in a black screen until the method finishes.
Does anybody have an idea, where and how I can properly initialize my app on startup?
Initialization code:
public void init() {
if (initialized) {
return;
}
// Initialize terms
List<Tag> tags= DynamicDao.loadAll(Tag.class);
int numTags = tags.size();
terms = new String[numTags];
for (int i = 0; i < numTags; i++) {
terms[i] = tags.get(i).getTag();
}
// Initialize document-term matrix
List<Item> items = DynamicDao.loadAll(Item.class);
createDocumentTermMatrix(items);
initialized = true;
}
Note: An Item has several associated tags, from which I need to create a document vector.
Upvotes: 7
Views: 13029
Reputation: 1201
If you want to keep your current splash screen, you have a couple of options.
If your data structure isn't too colossal, you can store it in onSaveInstanceState and restore it in onRestoreInstanceState and/or onPostCreate.
If the data is too large, you may just need to check to see whether your app is initialized in onResume or one of the other variety of startup methods like onRestart, onStart, etc. (I'm still a little hazy on when exactly each should be used.) If not, start your splash screen Activity.
The advice from others on this topic is good, as well. But this may work for you if you need a quick fix.
Upvotes: 0
Reputation: 113
If you really have to execute a long lasting operation then you should use AsyncTask. It's really simple to use, it provides you with two functions called onPreExecute and onPostExecute which are invoked in the main thread respectively before and after the operation. All the expensive stuff should go in doInBackground which will work in the worker thread.
While you are doing that operation you could show a progress dialog (creating it inside the above mentioned onPreExecute) showing the progress of what you are doing by using one of the callback provided: onProgressUpdate you will then dismiss the dialog in the above mentioned inside onPostExecute
Upvotes: 2
Reputation: 77732
Just how expensive is your initialization? What do you do there? In general, I would recommend against using a splash screen (it's a mobile app, not a desktop application). Instead, use a worker thread to initialize your data while the main UI is being displayed, and then use handler to initialize the UI once your worker thread is done.
Alternatively, I would look into why your initialization is taking so long, and optimizing it. What are you doing there?
Upvotes: 3