title: In what android activity state should one be opening and closing database connections?
body: <p>I have a commercial application that is almost finished. It has 8 list activities and 4 detail activities and a bunch of other good stuff going on.</p>
  
  <p>When I started out (and was new to Android development), in each method that I needed a database connection, I created a new SQLiteOpenHelper subclass instance and database connection and then closed the connection at the end of the method. This seemed to do bad things.</p>
  
  <p>Complicating everything is that I need to install my data upon first launch. I have a REST app to access and have cached some data from it. I seem to run out of memory when I try to load one of the tables even when I use the JSONReader class in GSON and not just regular InputStreamReader functionality. So, it is supposed to read in objects one at a time. But it still runs out of memory on emulators with &lt;= 1 GB memory.</p>
  
  <p>So, then I started using a singleton, with a synchronized getter, for the SQLiteOpenHelper subclass instance. Doing this, should I still be able to get db connection objects and close them within my methods? This did not seem to work.</p>
  
  <p>Now I am often using a SimpleCursorAdapter subclass for the list activities. This seems to move the problem a bit but not fix it or make it worse.</p>
  
  <p>See this diagram of the activities and when they run:</p>
  
  <p><a href="http://developer.android.com/images/activity_lifecycle.png" rel="nofollow">http://developer.android.com/images/activity_lifecycle.png</a></p>
  
  <p>Seeing this, I think I should be able to create the database connection in the onStart call and close it and null the db connection object in the onStop call. No? When I am using a SimpleCursorAdapter subclass, I am loading the adapter in the onStart call and changing the cursor for null in the onStop call. Is this wrong? Apparently it is.</p>
  
  <p>I have a detail activity with the db connection ivar in the class scope. I open the db connection in onStart and close it in onStop. So, why am I getting a "java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase" error in my onClick method? The onClick gets called between the onStart and onStop calls, yes? So what the heck gives?</p>
  
  <p>Right now I have a helper method which takes the db connection instance as a parameter and checks it and either returns it, if it is open, or sends back a new, open instance if not.</p>
  
  <p>So, should I need to call this method every 5 or 10 lines in my code? I do not think so, but that is how it feels. I am <em>only</em> closing the db connection objects in onStop methods. Why should this be a problem? Should I just not close anything? It will leak, but it may not crash. But that seems wrong. Hmph.</p>
  
  <p>How many db connection objects do I really have? Only one in the entire app? Only one per thread? Some other number? How do I close them so that they are not leaked without causing crashes?</p>
  
  <p>Any other suggestions?</p>