How to force a content provider to reset when manually deleting database
Asked Answered
S

3

8

In my app I have a ContentProvider attached to a table in a database with a CursorLoader that fills a ListView in one of my Activities. This table is filled empty by default and gets filled with user input data. I want to allow the user to completely delete all of their stored data and I'm deleting the entire database when this option is selected. The database is then recreated in it's default state when the user starts using the app again, just as it would the first time they used the app.

My issue is when I delete the database, the ContentProvider doesn't detect that the database was deleted and when I go back to my listview activity, the list is still there. I'm also making the app completely reload the ListView Activity instead of just resuming from memory and the list is still there even though the database is empty. The only way I can get the ContentProvider to reload is to kill the app in the system settings and then open it again.

Is there a way to forcefully restart the ContentProvider or to tell it that the data has been updated from outside of the ContentProvider class itself?

Sprint answered 19/4, 2013 at 21:30 Comment(0)
S
1

I found the solution to my problem here: https://mcmap.net/q/1118519/-refresh-reload-database-reference-in-custom-contentprovider-after-restore

I was getting my dbHelper in the onCreate of my ContentProvider class just like in the other post and getting a new dbHelper in each method fixed my issue.

Sprint answered 22/4, 2013 at 6:17 Comment(0)
T
2

You can also simply use the delete method of the content provider without defining a selection:

context.getContentResolver().delete(YourProvider.CONTENT_URI, null, null);
Templar answered 13/10, 2015 at 13:15 Comment(1)
I doubt this solution, Please show use why you believe this works.Tica
S
1

I found the solution to my problem here: https://mcmap.net/q/1118519/-refresh-reload-database-reference-in-custom-contentprovider-after-restore

I was getting my dbHelper in the onCreate of my ContentProvider class just like in the other post and getting a new dbHelper in each method fixed my issue.

Sprint answered 22/4, 2013 at 6:17 Comment(0)
P
0

You want to populate the listview with a cursorAdapter, and notify the cursor when the content changed in the contentProvider.

In your delete method of your contentProvider implementation, call contentResolver.notify . This will notify the cursor populated by the call to contentResolver that there has been a change in the data.

Phenformin answered 19/4, 2013 at 21:34 Comment(3)
Yeah, that's what I'm doing. The problem here is the ContentProvider is only attached to one table of my database and a separate Activity that doesn't use the provider deletes the entire database when the user wants to through the getApplicationContext().deleteDatabase(myDb). Is there a way to send a trigger to the provider to restart/refresh it when I don't have direct access to the contentResolver? I'm currently using the delete method in my provider to delete individual rows from the table that's used for my listview/cursoradapter, as you suggest.Sprint
If you are using a loader to load the listview, you can also just call restart on the loader right after you delete your databasePhenformin
I was just playing around with it and this didn't fix my issue. Through some more creative searching, I found this issue and the solution worked: https://mcmap.net/q/1118519/-refresh-reload-database-reference-in-custom-contentprovider-after-restoreSprint

© 2022 - 2024 — McMap. All rights reserved.