user1214317
user1214317

Reputation:

Closing a Database in onPause()

Is it best practice to close your database when the activity is paused? Or is it safe to leave the database open regardless of the state of the activity?

Curious to know because if i close my database in onPause() then try to reopen it in onResume() it throws a null pointer exception and says trying to re query a database from an already closed cursor. . .

Upvotes: 1

Views: 1403

Answers (3)

Yury
Yury

Reputation: 20946

Can you post the error? I usually close database in onPause method and open it in onResume. Because I've read that onDestroy is not always called. But in your case the problem is, I think, in a Cursor that is not closed. But I'm not sure that's why I ask you to post logcat and your code.

Upvotes: 0

WarrenFaith
WarrenFaith

Reputation: 57702

I never close my database and I open it in the onCreate of my custom Application class implementation.

There is a onTerminate method but the documentation says that it never will be executed, so there is no real way to find out when the application will be terminated.

I never experienced any problems with the never close database pattern.

Upvotes: 0

Thommy
Thommy

Reputation: 5417

Closing it in onDestroy() is imho the best way.

Upvotes: 3

Related Questions