When I am dealing with a usual Activity, then I open a database in onCreate and close it in onStop. When I deal with tab activities, then I add another open in onResume and another close in onPause.
So the code looks like this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDB.open();
}
@Override
protected void onResume() {
super.onResume();
mDB.open();
}
@Override
protected void onPause() {
super.onPause();
mDB.close();
}
@Override
protected void onStop() {
super.onStop();
mDB.close();
}
Is this too much of opening and closing?
I don’t think so. You are just managing lifecycle of the application, which is a very good thing.