В общем, я бы закрыл соединение в функции onDestroy () Activity, которая открыла соединение. Я бы закрыл () курсор из базы данных в функции, которая использует курсор.
public MyActivity extends Activity{
private myDatabase mDatabase; // myDatabase extends SQLiteOpenHelper
private Cursor mCursor;
public MyActivity(Context context){
super(context);
initMemberVariables();
}
public ElementButton(Context context, AttributeSet attrS){
super(context, attrS);
initMemberVariables();
}
public ElementButton(Context context, AttributeSet attrS, int defStyle){
super(context, attrS, defStyle);
initMemberVariables();
}
private void initMemberVariables(){
mDatabase = new PSEdb(this.getContext());
}
private void getData(){
mCursor = mDatabase.MyGetterFunction();
while(mCursor.moveToNext()){
try{
// populate your data
}catch(CursorIndexOutOfBoundsException ex){
// handle the exception
}
}
mCursor.close();
}
@Override
public void onDestroy(){
super.onDestroy();
mDatabase.close();
}
}