Skip to content Skip to sidebar Skip to footer

Room Data Is Lost After My App Is Restarted

I have a simple implementation of Room in my app. It has two entities. It works fine while app is running. But I see no data once app is restarted. This is my database creation cod

Solution 1:

The reason why you had an empty db every time you restart the app is because in your database class you used "Room.inMemoryDatabaseBuilder.." which is used only for testing purposes on db ,so insteand you should used "Room.databaseBuilder.."

Solution 2:

It was a silly mistake-

I wasn't using getInstance() posted in question, I was using below code to create database instead -

mDb = Room.inMemoryDatabaseBuilder(mContext, AppDatabase.class)

My code works as intended with original code posted in question.

Thanks

Solution 3:

you used below code and i think your issue will be solved .. make data base like below code ..

@Database(entities = {MyTable.class}, version = 1)

public abstract class AppDatabase extends RoomDatabase { public abstract MyTableDao getTableDao(); }

then after make dao interface..

@Dao

public interface MyTableDao { @Insert void insertData(MyTable myTable);

}

and make your pojo class

@Entity

public class MyTable { @PrimaryKey(autoGenerate = true) private int id;

publicintgetId() {
    return id;
}

publicvoidsetId(int id) {
    this.id = id;
}

privatedouble ItemPrice;

publicdoublegetItemPrice() {
    return ItemPrice;
}

publicvoidsetItemPrice(double itemPrice) {
    ItemPrice = itemPrice;
}

}

than after make one app activity class and called at the application level.

publicclassAppActivityextendsApplication {

static AppDatabase db;

@OverridepublicvoidonCreate() {
    super.onCreate();
    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
}

publicstaticAppDatabasegetDatabase() {
    return db;
}

}

above activity class declared in manifest file in application tag.

        android:name=".AppActivity"

then after used to insert in main activity like this ..

        AppActivity.getDatabase().getTableDao().insertData(yourobject);

Solution 4:

I didn't check my code but may be it will work for you. Try this:

@Database(entities = {Person.class, College.class}, version = 1)

publicabstractclassAppDatabaseextendsRoomDatabase {
    privatestatic AppdDatabase INSTANCE;
    publicabstract PersonDao personDao();
    publicabstract CollegeDao collgeDao();

    publicstatic AppDatabase getInstance(final Context context) {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "my_sample_app")
                               .build();
            }
        }        
        return INSTANCE;
    }

    publicstaticvoiddestroyInstance() {
        if (INSTANCE != null) INSTANCE.close();
        INSTANCE = null;
    }

}

Post a Comment for "Room Data Is Lost After My App Is Restarted"