I want to provide common functionality between different activity types such as opening and closing a database connection. Consider the following class:
public class DataActivity extends Activity {
private DbAdapter mDbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//open database
mDbAdapter = new DbAdapter(this);
mDbAdapter.open();
}
@Override
protected void onDestroy() {
super.onDestroy();
//close database
mDbAdapter.close();
}
public DbAdapter getDbAdapter() {
return mDbAdapter;
}
}
I could then simply extend DataActivity
and have access to my data throughout the class. However, what if I wanted to do this for a ListActivity
or an ExpandableListActivity
? I would need to make a copy of DataActivity
and have it extend the desired class.
This seems really messy having multiple classes with duplicate implementation code for each activity type I would like to add this to.
Edit It looks like what I am trying to do above isn't really possible due do the way Java is designed (no multiple inheritance). There are several ways to minimize duplication of code but inheritance just doesn't work in this case.