ContentProvider not called onCreate after deleting database
Asked Answered
S

1

1

I have created ContentProvider which creates one Database on application launching.

Now In that application, I am doing process of deleting database when user logout from app.

After that when I come again to login, the ContentProvider cant call onCreate() of overrided class.

Is there any way to recreate database using ContentProvider?

Soho answered 15/2, 2017 at 12:47 Comment(1)
You do something strange... database should not work in such way. Who delete whole database on user logout? If there are some specific data related only to the login - then delete the data only. Just clear the temporary tables... its way better then delete whole database on every user logout and recreate it on every user login :)Alfi
S
2

I found solution as,

First of all I referred Refresh/Reload database reference in custom ContentProvider after restore but not satisfied with answer because its just for closing database.

So I have created my answer as below:

DBHelper.java

/**
 * Delete database
 */
public static void reCreateDatabase(Context mContext) {
    ContentResolver resolver = mContext.getContentResolver();
    ContentProviderClient client = resolver.acquireContentProviderClient(KOOPSContentProvider.AUTHORITY);

    assert client != null;
    KOOPSContentProvider provider = (KOOPSContentProvider) client.getLocalContentProvider();

    assert provider != null;
    provider.resetDatabase();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        client.close();
    else
        client.release();

    LOGD("Database Deleted...");
}

public void removeDatabase(Context mContext) {
    mContext.deleteDatabase(DATABASE_NAME);
    LOGD("Database Deleted...");
}

KOOPSContentProvider.java

public void resetDatabase() {
    if(dbHelper != null) {
        dbHelper.removeDatabase(getContext());
        dbHelper = new DbHelper(getContext());
    } else {
        LOGD("Database NULL");
    }
}

USE As:

DbHelper.reCreateDatabase(mContext);

Thank you :) :)

Soho answered 16/2, 2017 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.