vendredi 29 janvier 2016

Creation of databases at run time in Android

I am using a DataProvider for my app to handle CRUD operations. Now the DataProvider is using an SQLiteOpenHelper nested inner class. All definitions of the CRUD operations are defined inside the DataProvider itself and the SQLiteOpenHelper class is responsible for creation of database, say abc.db and defining two tables say table1 and table2. Now, when a user logs in to the app, the database is created (obviously if not exists) and the data for the user is displayed by using ContentResolver queries and CursorLoader for loading into lists. This part I have achieved.

The problem arises in a multi-user scenario. Suppose a user decides to logout and login with another account or register for a new account, as the data for the previous user is retained, the previous data is also loaded into the ListView and/or other views fetching data from the database. According to my logic, this can be prevented by creating new database instances for each new user on the same device. But since the ContentProvider initialises the database prior to Activity's onCreate(), I find this hard to achieve. Any idea on how it can be implemented?

Aucun commentaire:

Enregistrer un commentaire