Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing an Android application

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.

like image 394
Matthias Avatar asked Sep 10 '10 17:09

Matthias


People also ask

How do I launch an Android app?

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.

How do I change the startup programs on my Android?

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.

What is init Android studio?

Initializer s can be used to initialize libraries during app startup, without the need to use additional android. content.


1 Answers

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?

like image 125
EboMike Avatar answered Sep 29 '22 11:09

EboMike