lundi 2 mai 2016

Android SQLite OpenHelper empty DB on Restart App

I am trying to complete an Android App, using SQLiteOpenHelper to Manage the DB. (With singleton)

private static WindAppDBHelper sInstance;

    public static synchronized WindAppDBHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new WindAppDBHelper(context.getApplicationContext());
        }
        return sInstance;
    }

I don't understand why the DB is working very well on RunTime, but after close the app and open again, the DB is (at least it looks) empty?? For example I made a test on my Activity (I have only one activity and I use the db inside my Fragments (Tabs))

Before quit, using debug tools I saw that dbHelper.getAllFavourites() has size=2; (I know, it is a very stupid code, but it is only to test, even the onClose is not necessary if everything goes well with DBHelper...)

@Override
protected void onStop() {
    List<Favourite> test = dbHelper.getAllFavourites();
    test.size();
    dbHelper.close();
    test.size();
    super.onStop();
}

After that, when I open the App again, inside the same Activity but inside onCreate and test.size=0... why?

    dbHelper = WindAppDBHelper.getInstance(this);
    List<Favourite> test = dbHelper.getAllFavourites();
    test.size();

Thanks in advance!!

Aucun commentaire:

Enregistrer un commentaire