only close the database when the app actually quit or not active.