java 初始化 Android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3686788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:00:31  来源:igfitidea点击:

Initializing an Android application

javaandroidsdkandroid-activity

提问by Matthias

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.

我目前面临以下问题:每当我的 Android 应用程序启动时,它都需要执行一些耗时的初始化代码。如果没有此代码,我在应用程序中的所有活动/服务都将无法正常工作。

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.

到目前为止,我已将此初始化代码放入一个 SplashScreen 活动中,我在清单中将其声明为 MAIN 活动。一旦初始化代码被执行,我就完成()启动屏幕并启动实际的主要活动,即由几个选项卡组成的活动,用户可以从那里访问几个其他活动。

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.

现在的问题如下:当我的应用程序置于后台时,经过一段时间并启动其他应用程序后,我的应用程序/进程被终止。当我从主屏幕重新启动它时,Android 会恢复活动堆栈(任务)并对它们调用 onCreate()。但是,不会执行初始屏幕活动以及初始化代码,这会导致异常。

I could put now the initialization code in the application's onCreate(), however this results in a black screen until the method finishes.

我现在可以将初始化代码放在应用程序的 onCreate() 中,但是这会导致黑屏,直到方法完成。

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.

注意:一个 Item 有几个关联的标签,我需要从中创建一个文档向量。

采纳答案by EboMike

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.

您的初始化成本有多高?你在那儿做什么?一般来说,我建议不要使用启动画面(它是一个移动应用程序,而不是一个桌面应用程序)。相反,在显示主 UI 时使用工作线程初始化数据,然后在工作线程完成后使用处理程序初始化 UI。

Alternatively, I would look into why your initialization is taking so long, and optimizing it. What are you doing there?

或者,我会调查为什么您的初始化需要这么长时间,并对其进行优化。你在那里做什么?

回答by lucabox

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 onPreExecuteand onPostExecutewhich are invoked in the main thread respectively before and after the operation. All the expensive stuff should go in doInBackgroundwhich will work in the worker thread.

如果你真的必须执行一个持久的操作,那么你应该使用AsyncTask。使用起来真的很简单,它为您提供了两个函数,称为onPreExecuteonPostExecute,分别在操作前后在主线程中调用。所有昂贵的东西都应该放在doInBackground 中,它会在工作线程中工作。

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: onProgressUpdateyou will then dismiss the dialog in the above mentioned inside onPostExecute

当您执行该操作时,您可以显示一个进度对话框(在上面提到的onPreExecute 中创建它),通过使用提供的回调之一来显示您正在执行的操作的进度:onProgressUpdate然后您将在上面提到的onPostExecute 中关闭对话框

回答by BenTobin

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.

如果您的数据结构不是太庞大,您可以将其存储在 onSaveInstanceState 中并在 onRestoreInstanceState 和/或 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.

如果数据太大,您可能只需要检查您的应用程序是否在 onResume 或其他各种启动方法(如 onRestart、onStart 等)中初始化。应该使用。)如果没有,请启动您的初始屏幕活动。

The advice from others on this topic is good, as well. But this may work for you if you need a quick fix.

其他人在这个主题上的建议也很好。但是,如果您需要快速修复,这可能对您有用。