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.
An Android process is started whenever it is required. Any time a user or some other system component requests a component (could be a service, an activity or an intent receiver) that belongs to your application be executed, the Android system spins off a new process for your app if it's not already running.
To give this method a try, open Settings and go to the Application Manager. It should be in "Installed Apps" or "Applications," depending on your device. Select an app from the list of downloaded apps and turn the Autostart option on or off.
Initializer s can be used to initialize libraries during app startup, without the need to use additional android. content.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With