1

I have an activity where I am opening an database by calling constructor of SQLiteOpenHelper.

dbHelper = new DBHelper(this); //DBHelper class extends SQLiteOpenHelper

Then on click of a button I am opening another activity and closing the dbHelper.

int cuisineId = HelperUtils.getCuisineIdByName(dbHelper,cuisine);
SingletonGlobalClass.getInstance().setCuisineId(cuisineId);
String restaurantNameSearchStr = restaurantName.getText().toString().trim();
Intent intent = new Intent();
intent.setComponent(new ComponentName("ctnxa.android", ctnxa.android.SearchResultActivity"));
intent.putExtra("searchStr", restaurantNameSearchStr);
intent.putExtra("option", R.string.restaurant);
startActivity(intent);
dbHelper.close();

Now when I am pressing back button it comes back to this activity. When I am trying to click the search button again it uses the dbHelper and works as usually without any error. My expectation is it should not work as the dbHelper has already been closed before and it can only be re-instantiated when activity onCreate() method is called which is not getting called in this case as I am only pressing back button (It should call onResume()). I am really confused. Could any body explain what is happening here.

Edit: This is how I have implemented dbHelper.close()

@Override
public synchronized void close() {
    super.close();
}
Alex Lockwood
  • 81,274
  • 37
  • 197
  • 245
Sush
  • 6,259
  • 1
  • 15
  • 26
  • in the close() method for DBHelper class are you properly closing the database ? Please print the close() method . And how did you declare your first Activity in the manifest any extra options ? like launchMode ... – Rick Dec 25 '11 at 20:31
  • @Rabgs: Updated question with the close method. – Sush Dec 25 '11 at 20:45
  • You should call the SQLiteDatabase close method in the DBHelpber close method . Example here http://code.google.com/p/android-notes/source/browse/trunk/src/com/bitsetters/android/notes/DBHelper.java?r=10 – Rick Dec 25 '11 at 20:51
  • As per developer documents SQLiteOpenHelper.close() should Close any open database object. So the call to dbHelper.close() should close any open database object. – Sush Dec 26 '11 at 04:18

2 Answers2

2

When you start a new Activity, the first Activity may or may not go through the various 'exit' stages but there is no guarantee which ones it will go through. It depends entirely on the available resources and other factors on an individual device.

The first Activity might simply pause and when you return it will resume. However, the first Activity could very likely be stopped or even destroyed. There is no guarantee what the Android OS will do with the first Activity.

In your case it seems the first IS actually being destroyed and re-created when you return from the second Activity.

The only way to verify this is to override all of the methods such as onPause, onStop and onDestroy and have each of them use Log to show which stages the first Activity goes through.

Squonk
  • 47,603
  • 18
  • 99
  • 134
  • I tested by putting breakpoints to see if it is getting recreated. But no it is not getting recreated. – Sush Dec 25 '11 at 20:44
  • OK, so what does `HelperUtils.getCuisineIdByName(dbHelper,cuisine)` do? From what you explain, `dbHelper.close()` doesn't release / invalidate `dbHelper` itself - it will still reference the instance of your `SQLiteHelper`. If `HelperUtils.getCuisineIdByName(...)` calls `getReadbleDatabase()` or `getWritableDatabase` then the behavour makes sense. – Squonk Dec 25 '11 at 20:59
  • yes. it calls getReadableDatabase().rawQuery implicitly. Then please explain how it makes sense. – Sush Dec 26 '11 at 03:55
  • Ok. may be I understand what you are trying to explain. As per documents,call to getReadableDatabase() Creates and/or opens a database. So it re-opens the database. Is it correct? – Sush Dec 26 '11 at 04:04
  • 1
    Yes, correct. Calling dbHelper.close() simply closes the database but if you are saying that the first `Activity` is not destroyed the `dbHelper` will still hold a valid reference when you return from the second `Activity`. As long as that is the case, the call to `HelperUtils.getCuisineIdByName(dbHelper,cuisine)` will get another reference to the database because it is calling `getReadableDatabase` in order to call `rawQuery`. Do, not expect this behaviour to be consistent though - as I said in my answer, you cannot rely on the first `Activity` to survive after you start the second. – Squonk Dec 26 '11 at 08:29
  • Thanks. It helped me a lot to understand the concept. – Sush Dec 26 '11 at 17:08
1

you can avoid a lot of this confusion by ensuring that there exists only one SQLiteOpenHelper throughout the Application's life-cycle. check out this answer for more info.

Community
  • 1
  • 1
Alex Lockwood
  • 81,274
  • 37
  • 197
  • 245