0
Android Studio 0.4.4

Hello,

I have this onCreate in my main_activity class:

DBHelper dbHelper;
SQLiteDatabase db = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbHelper = new DBHelper(this);

    /* Database hasn't been created yet, if already created and opened ignore */
    if(db == null) {
        db = dbHelper.getWritableDatabase();
        Log.d(TAG, "onCreated() Create database for the first time");
    }
}

I have the dbHelper class that calls onCreate, onUpdate, etc. However, as my application will use portrait and landscape, each time the user switches it will call the onCreate method. I don't want to have to keep creating the same database each time. I tried checking for a null, but this didn't work.

I cannot disable the calling onCreate in the manifest file (android:configChanges), as I have savedInstanceState that I will need in this method each time the user switches.

Many thanks for any suggestions,

public class DBHelper extends SQLiteOpenHelper {

    static final String DB_NAME = "friends.db";
    static final int DB_VERSION = 1;
    static final String TABLE = "friends";
    static final String C_ID = BaseColumns._ID;
    static final String C_NAME = "name";
    static final String C_PHONE = "phone";
    static final String C_EMAIL = "email";

    private final String TAG = "DBHELPER";

    // Constructor
    public DBHelper(Context _context) {
        super(_context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TABLE + "(" + C_ID + " int primary key, "
                + C_NAME + " text, "
                + C_PHONE + " text, "
                + C_EMAIL + " text)";

        db.execSQL(sql);
        Log.d(TAG, "onCreate sql: " + sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "drop table if exists " + TABLE;

        // Drop old database if it exists
        db.execSQL(sql);
        // Create a new database
        Log.d(TAG, "onUpgrade() sql: " + sql);
        onCreate(db);
    }
}
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • check this link. i think it's helpful for you. http://stackoverflow.com/questions/8007993/in-android-check-if-sqlite-database-exists-fails-from-time-to-time – Jigar Shekh Feb 12 '14 at 16:34
  • thats why i'm using ContentProviders ... but still you can store DBHelper instance in Application instance ... or as some kind of Singleton `DBHelper.getInstance(applicationContextNotActivityContext)` – Selvin Feb 12 '14 at 16:34
  • check this link. http://stackoverflow.com/questions/3058909/how-does-one-check-if-a-table-exists-in-an-android-sqlite-database – Jigar Shekh Feb 12 '14 at 16:37
  • @JigarShekh seems like you do not understand the question(maybe becuase the question itself is not clear) i think that ant2009 is talking about instance of helper/db class noth db file ... ant2009 check http://stackoverflow.com/questions/6905524/using-singleton-design-pattern-for-sqlitedatabase – Selvin Feb 12 '14 at 16:38
  • Activity creations is after all a rare event. What's the problem having to recreate your db helper object as well? – laalto Feb 12 '14 at 16:41

0 Answers0