gpt4 book ai didi

Android 在应用程序级别关闭和打开 SQLite 数据库

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:01 25 4
gpt4 key购买 nike

我在应用程序级别声明和存储了一个 SQLite 数据库。我处理设备旋转等配置更改,因此关闭数据库我将其设置在 MainActivity 的 onDestroy 中,但在 if(this.isFinishing()) 中。

我遇到的问题是,如果在关闭应用程序后我再次尝试启动它,它会崩溃并告诉我它试图打开一个已经关闭的数据库。下次它将正确启动。我猜这是因为 Android 从来没有真正关闭过应用程序,因为没有必要,它仍然在后台。

我找不到一种方法来检测 onFinish/onDestroy.... 在应用程序级别关闭数据库。我能做的唯一解决方法是不关闭数据库。但这可能会给我带来问题,当我尝试再次打开它时,以防 Android 真的关闭了我的应用程序,不是吗?

有没有办法强制关闭应用程序? (我认为没有)。有没有办法检测应用程序的关闭以关闭那里的数据库?如果应用程序被 Android 关闭,我在尝试打开未关闭的数据库时会遇到问题吗?或者 Android 关闭了链接到它要关闭的应用程序的数据库?

最佳答案

The only workaround I can do is to not close the database

正确,这是正确答案。

But this can give me problems maybe when trying to open it again in case Android truly closed my Application, no?

没有。

Is there a way to detect the closing of the Application to close the database there?

没有。

Will I have problems trying to open a not closed database if the Application was closed by Android?

没有。

Or Android closes the database linked to an Application that it is going to close?

没有。

关闭数据库实际上并没有那么多作用。 SQLite 是事务性的;当您执行这些事务时,您所有的数据库写入操作都会写入磁盘。

关于Android 在应用程序级别关闭和打开 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25020110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com