Sharing common functions between different types of operations

I want to provide general functionality between various types of operations, 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;
    }

}

Then I could just expand DataActivityand access my data throughout the class. However, what if I wanted to do this for ListActivityor ExpandableListActivity? I need to make a copy DataActivityand expand it.

This seems really messy if you have several classes with duplicate implementation code for each type of activity to which I would like to add this.

Edit , , , , - , Java ( ). , .

+3
1

( dun ) ( BaseActivity, BaseMapActivity, BaseXXXActivity..) , : vs . Android Java, , Java.

: : `private DbAdapter dbAdapter = DbAdapter (this);

onCreate open() onDestroy close(). getAdapter(). .

, DataService, instance(Context ctx) - DbHelper.

0

Source: https://habr.com/ru/post/1776831/


All Articles