I was advised in a previous question that I didn’t need to maintain the database if the device is rotated as it uses a local database.
I thought that I could at least maintain the datasource for my List. I am having problems trying to do this because I get an error saying close() was not called on the database. I am calling close in the onDestroy though.
Can anyone see where I am going wrong here? I just want to improve the users experience by maintaining the data to repopulate the list after they rotate the device.
The other thing that is confusing me is my log statements come out in following order:
- onCreate flights
- first setup
- reusing datasource
- destroying activity
I would have expected the destroying activity to happen before reusing datasource.
package com.testing.flights;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.SimpleCursorAdapter;
public class FlightListActivity extends ListActivity {
private SQLiteDatabase database;
private String fields[] = {BaseColumns._ID, "name", "flights", "distance"};
private SimpleCursorAdapter datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(ACTIVITY_SERVICE, "onCreate flights");
datasource = (SimpleCursorAdapter) getLastNonConfigurationInstance();
if (datasource == null) {
database = getData();
datasource = getCursor();
Log.v(ACTIVITY_SERVICE, "first setup");
}
setListAdapter(datasource);
}
protected SQLiteDatabase getData() {
DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
return myDbHelper.openDataBase();
}
protected CustomCursorAdapter getCursor() {
Cursor data = database.query("pilots", fields, null, null, null, null, null);
final CustomCursorAdapter mysource = new CustomCursorAdapter(this, R.layout.row, data, fields, new int[] { R.id.id, R.id.name, R.id.flights, R.id.distance });
return mysource;
}
@Override
public Object onRetainNonConfigurationInstance() {
Log.v(ACTIVITY_SERVICE, "reusing datasource");
final SimpleCursorAdapter myData = datasource;
return myData;
}
@Override
protected void onDestroy() {
Log.v(ACTIVITY_SERVICE, "destroying activity");
database.close();
super.onDestroy();
}
}
LogCat:
04-03 16:22:55.900: E/SQLiteDatabase(1860): close() was never explicitly called on database '/data/data/com.testing.flights/databases/club'
I believe what is happening is that the OS is prioritizing the creation of the new Activity over the destruction of the old one. Because onDestroy is not guaranteed to run (it can be killed) it is typical to open your database in
onResume(), and to clean up after yourself in theonPause()method. (onPause won’t be killed, at least in honeycomb + ). As a result of what you have now, the new activity is trying to open a database that’s already open (is my guess). Not having tried to retain a cursor like this, I’m not 100% that this will fix your problem, but using that part of the lifecycle is the normal case for non view setup type things.