How to check if a DB exists in Android?

后端 未结 4 587
广开言路
广开言路 2021-01-19 15:05

I am using Room API to implement a DB in my Android app. It seems that every time I load my app it tries to create the database again and again. Is there any way to restrict

4条回答
  •  一个人的身影
    2021-01-19 15:47

    when you create database it call when application start that time their db create.you used to below code in app activity and that activity call in manifest file in application class call like used below code ..

    public class AppActivity extends Application {
    
    AppDatabase db;
    
    @Override
    public void onCreate() {
        super.onCreate();
        db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
    }
    
    public AppDatabase getDatabase() {
        return db;
    }
    

    } and add below line manifest file .. add below line in application tag

            android:name="AppActivity"
    

提交回复
热议问题