Question

I have a SQLite database declared and stored at Application level. I handle configuration changes like device rotation, so the closing of the database I set it inside the onDestroy of the MainActivity, but inside an if(this.isFinishing()).

The problem I have is that, if just after closing the app I try to launch it again, it crashes telling me that it tried to open an already closed database. The next time it will launch correctly. I guess this is because Android never really closed the Application because there was no need, and it is still in background.

I couldn't find a way to detect onFinish/onDestroy.... at Application level, to close the database there. 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 force close the Application? (I think there isn't). 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?

Was it helpful?

Solution

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

Correct, and this is the right answer.

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

No.

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

No.

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

No.

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

No.

Closing a database does not really do all that much. SQLite is transactional; all of your database write operations are written to disk when you execute those transactions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top