有什么方法可以保证Onpause或Onstop Android生命周期事件直到最后?

发布于 2025-01-22 12:42:50 字数 374 浏览 2 评论 0原文

android文档,至少,至少,至少,至少,至少, Onpause无法杀死。也许我的解释是不正确的,但我知道它总是会直到最后。我已经进行了多个测试,并进行了一个漫长的循环周期,可以中断它的一半呼叫。

我想在呼叫中保存一些持久数据,但不幸的是,它可以中途中断。我知道我应该优化它,并且要做,但是我想保证我使用的生命周期电话会运行到最后。

In the Android documentation, it's stated that, at least, onPause is not killable. Maybe my interpretation is incorrect but I understood that it would always run until the end. I've made several tests with a lengthy loop cycle and I can interrupt it half call.

I want to save some persistent data in the call but unfortunately, it can be interrupted halfway through. I'm aware I should optimize it and I'm going to do it but I wanted to guarantee that the lifecycle call I'm using would run until the end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2025-01-29 12:42:50

如果将这些事件提出来“制动”它们的唯一方法是在结束前提出例外。
说“ onpause/onStop事件不是可杀死的”仅表示下面的代码(伪代码)将始终执行您的代码:

@Override
public void onPause/onStop() {
    try {
        --------------
        YOUR CODE HERE
        --------------
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    } finally {
        super.onPause/onStop();
    }
}

但是,Android文档说,在这些方法之后,这些方法可以是在任何时候,在任何情况下,在任何情况下都将执行Onpause/Onstop事件,无论如何在任何情况下都将被杀死。

如果您想安全地保存这些异步事件的持久数据,我建议您产生不同的过程,然后传递“序列化”(使用AIDL?)对象。遗忘了这个新的过程仅保留给这种执行。

If those events are raised the only way to "brake" them is by throwing an Exception before their end.
Saying that "onPause/onStop events are not killable" only means that this below code (pseudo-code) will always execute YOUR CODE:

@Override
public void onPause/onStop() {
    try {
        --------------
        YOUR CODE HERE
        --------------
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    } finally {
        super.onPause/onStop();
    }
}

However Android Docs says that AFTER these methods the Process could be killed in any time, but the onPause/onStop events will be executed from their begin to their end in any case.

If you want to be secure to save persistant data on those ASYNC events, I suggest you to spawn a different Process and pass "serialized" (using AIDL?) objects to it. Obliviously this spawned new Process is reserved to only this kind of execution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文