In your example, it appears that you are creating a new object in onDestroy
and not closing old ones. So every time you do
sqlAdapter helper = new sqlAdapter(StartScreen.this);
You are creating a new object and you'll have a ton of unclosed references and this is what is causing the warnings.
Instead, it looks like you may looking for a singleton design pattern which will allow you to maintain one object in your application and then close it in onDestroy
.
public void onDestroy() {
super.onDestroy();
// close database
final MySqlHelper helper = MySqlHelper.getInstance();
helper.getDatabase().close();
}
Otherwise it remains open until onDestroy
just like you're trying to do now. This of course should be used if appropriate and you're actively using it in your application. If you rarely use the database, you will be fine just closing it after each use.
The idea is that you're reducing database open / close calls if you do a moderate amount of them and will be more efficient in that use case.
Here is a very, very stripped down example of a singleton.
public class MySqlHelper extends SQLiteOpenHelper {
static MySqlHelper mInstance = null;
static SQLiteDatabase mDatabase = null;
public static MySqlHelper getInstance() {
if (mInstance == null) {
// call private constructor
mInstance = new MySqlHelper();
}
mDatabase = mInstance.getWritableDatabase();
while(mDatabase.isDbLockedByCurrentThread() || mDatabase.isDbLockedByOtherThreads()) {
// loop until available
}
return mInstance;
}
private MySqlHelper() {
// mContext here is just a placeholder for your ApplicationContext
// that you should have available to this class.
super(mContext, DATABASE_NAME, null, DATABASE_VERSION);
}
// all other filled out methods like onCreate, onUpgrade, etc
}
Now you can use this to implement your datasource
public class MyDataSource {
// Database fields
private final SQLiteDatabase mDatabase;
private final MySqlHelper mHelper;
public MyDataSource() {
mHelper = MySqlHelper.getInstance();
mDatabase = mHelper.getDatabase();
}
// add your custom methods
private int update(ContentValues values, String whereClause) {
int rowsUpdated = 0;
synchronized (mDatabase) {
rowsUpdated = mDatabase.update(MySqlHelper.TABLE_NAME, values, whereClause, null);
}
return rowsUpdated;
}
public int updateById(ContentValues values, int id) {
final String whereClause = MySqlHelper.COLUMN_ID + "=" + id;
return this.update(values, whereClause);
}
}