I'd like to perform one or two actions when the instance of the class is shut down or destroyed. I'm looking for something similar to the onDestroy in an activity.
EDIT
I've added my code where I indicate how I serve back a SQLiteDatabase from my Helper class. I use the finalize code to ensure the database is closed.
public class PMDBDatabase {
private static SQLiteDatabase DataBase = null;
private static PMDBHelper dbHelper = null;
public SQLiteDatabase getDatabase(Context ctx) throws SQLException {
if (DataBase == null) {
dbHelper = PMDBHelper.getInstance(ctx);
DBOpen();
} else
if(!DataBase.isOpen())
DBOpen();
return DataBase;
}
private void DBOpen() throws SQLException {
DataBase = dbHelper.getWritableDatabase();
}
public void close(){
if(DataBase != null) DataBase.close();
}
protected void finalize() throws Throwable {
try {
close();
} finally {
super.finalize();
}
}
}
Could you please help this newbie to Java/Android programming and indicate whether the implementation of finalize is correct?
Thanks a lot for your time,
Jean