Android SQLite database shared between activities

后端 未结 2 795
粉色の甜心
粉色の甜心 2021-01-31 12:10

What is the best way to share one SQLite DB between several activities? Tables from the DB are shown in ListView, and also deleting/inserting records is to be performed. I heard

2条回答
  •  有刺的猬
    2021-01-31 13:11

    You could do this implementing a BaseActivity class what is extended by all Activity classes in the application:

    public class BaseActivity extends Activity {
    
        protected static SQLiteOpenHelper database;
    
        @Override
        protected void onPause() {
    
                // TODO close database
    
                super.onPause();
        }
    
        @Override
        protected void onResume() {
    
                super.onResume();
    
                // TODO open database
        }
    }
    
    
    
    
    public class OneSubActitivy extends BaseActivity {
    
        // methods using database from BaseActivity
    }
    

提交回复
热议问题