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

标签 android sqlite application-lifecycle

我在应用程序级别声明和存储了一个 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/

相关文章:

iphone - 在 iPhone 应用程序中将日期保存到 SQLite3

android - 如何处理 Android 中涉及套接字的 Activity 生命周期?

c# - 当 IIS 6.0 上的 asp.net Web 应用程序 (asmx ws) 闲置数小时左右时会发生什么情况?

java - Glide 不在第一个 fragment "automatically"中显示图像

android - 如何在 Fragment 和未附加的 Activity 之间进行通信?

python - 使用 SQLAlchemy 继承参数的 SQLite 和 Postgres 部分索引支持

sql - 如何创建 SQL 索引以提高 ORDER BY 性能

android - ACTION_IMAGE_CAPTURE Intent : Avoid Activity being destroyed/process being killed

java - Android Studio - 编辑器中的 FileNotFound 异常

android - 如何制作小尺寸的android应用程序?