[英]how to check in onCreate that the previous android process of the same activity has been killed?
I have an activity in whose onCreate method an Init function is called (the function calls some native code involving lot of stuffs and calls to the openSLES audio api). 我有一个活动,在该活动的onCreate方法中调用了一个Init函数(该函数调用一些涉及很多内容的本机代码,并调用openSLES音频api)。 The point is that this Init function makes the app crash when called again, which happens on a screen rotation or when i close the activity using Back button and i launch it again (but if in the meanwhile the process is killed, i have no troubles).
关键是,此Init函数会使应用程序在再次调用时崩溃,发生在屏幕旋转或当我使用``后退''按钮关闭活动并再次启动它时发生的情况(但是与此同时,如果该进程被终止,则我没有任何麻烦)。 I can't change the beaviour of the Init function.
我无法更改Init函数的行为。
I see that the process isn't killed when the activity is destroyed, I expected this after reading the docs, and it's a good thing since - if there is some audio signal playing - that continues playing after the activity has been destroyed, which is good for my purposes. 我看到活动被销毁后进程并未终止,我希望在阅读文档后能够做到这一点,这是一件好事,因为-如果正在播放一些音频信号-活动被销毁后仍会继续播放,这是对我的目的很好。
I tried to perform a check on the initialization state using onSaveInstanceState, but that works well only on screen-rotation, that's when onSaveInstanceState is called. 我尝试使用onSaveInstanceState对初始化状态进行检查,但仅在屏幕旋转时(即调用onSaveInstanceState时)才能很好地工作。 The callback is not called when i push the Back button.
当我按下“后退”按钮时,不会调用该回调。
So i tried to use Shared Preferences, performing the state saving in onPause. 所以我尝试使用共享首选项,在onPause中执行状态保存。 But at this point i have the opposite problem: if the process is killed, the Shared Preferences values are kept, but in that case i need to perform Init again for the app to work properly.
但是在这一点上,我遇到了相反的问题:如果进程被终止,则共享首选项值会保留,但是在那种情况下,我需要再次执行Init才能使应用正常运行。
I guess i need a way to know for sure if my activity is created after a process kill or not, but at the moment i can't see how. 我想我需要一种方法来确定我的活动是否在进程终止后创建,但是目前我看不到。 I thought about using the bundle instance in onPause method, but i can't figure how and whether this is possible.
我考虑过在onPause方法中使用bundle实例,但是我不知道如何以及是否可行。 Any kind of hint would be really appreciated.
任何提示将不胜感激。
edit: 编辑:
Ie declare following class: 即声明以下类:
public class YourApplication extends Application {
static private native synchronized void InitOpenSLES();
public YourApplication() {}
// see http://developer.android.com/reference/android/app/Application.html#onCreate() for details
@Override
public void onCreate() {
super.onCreate();
InitOpenSLES();
}
}
For each and every process you have pid or process id. 对于每个进程,您都有pid或进程ID。 In your init function you can easily get the thread id and can save it in any integer value.
在您的init函数中,您可以轻松获取线程ID并将其保存为任何整数值。
Thread.currentThread().getId())); Thread.currentThread()。getId()));
whenever your activity will restart just check that thread id is same or different. 每当您的活动重新开始时,只需检查线程ID相同或不同即可。 If thread id is different then call your function init function.
如果线程ID不同,则调用函数init函数。 Otherwise you have already done.
否则,您已经完成了。
There's a simple solution to this problem. 这个问题有一个简单的解决方案。 You don't need to save things in SharedPreferences to accomplish this.
您无需在SharedPreferences中保存内容即可完成此操作。 Just use a static (class) variable.
只需使用静态(类)变量。 Like this:
像这样:
public class Globals {
public static boolean initialized = false;
}
The variable initialized
will be set to false when the class is loaded. 加载类时,
initialized
的变量将设置为false。 Only once. 只有一次。 In your code, you then check and set the variable like this:
在代码中,然后检查并设置变量,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize (but only once per process)
if (!Globals.initialized) {
init(); // Call init function that does things one time per process
Globals.initialized = true; // Remember we are initialized so we don't
// do it again
}
...
}
Even if all your activities are finished, if the OS doesn't kill your process the variable initialized
will still be "true" if the application is started again. 即使您的所有活动都已完成,但如果操作系统没有终止您的进程,则如果再次启动该应用程序,则
initialized
的变量仍将为“ true”。 Once the OS kills the process, the variable will be set to "false" the next time the application is started and a new process is created. 一旦OS终止了进程,则下次启动应用程序并创建新进程时,变量将设置为“ false”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.