ContentProvider is not called onCreate after deleting the database

I created a ContentProvider that creates one database when the application starts.

Now in this application, I am doing the process of deleting the database when the user exits the application.

After that, when I log in again, ContentProvider cannot call onCreate () of the overridden class.

Is there a way to recreate a database using ContentProvider?

0
source share
1 answer

I found a solution like

First of all, I referred to the Refresh / Reload database link in the custom ContentProvider after recovery , but was not satisfied with the answer because it was just for closing the database.

So, I created my answer as shown 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); 

Thanks:):)

+1
source

All Articles