Lab: Persistence
We could serialize our GSON and store in shared preferences, but we're going to store and query a DB instead.
Steps
- Start with the Android tutorial to implement your DB contract.
- Continuing with the tutorial, create your DB helper class, database, and tables.
- Store your downloaded data in your newly minted DB using
ContentValues
.
- Create an
AsyncTaskLoader
to load your data from the DB. The documentation gives a good example.
- Implement
LoaderManager.LoaderCallbacks
and display your data after fetched.
- Revisit your logic for when you are fetching data from the network.
- Store in the shared preferences the fetched date and display in the log when the data was last fetched.
- Normally there'd be a last updated date in the JSON that you can compare your data set against.
- Store the last tab the user was viewing in the shared preferences and show when restoring the view.
Note: Start small with one fragment/set of data and make sure that works before moving on to making both work.
Note: Remember to test early and test often.
Resources
SQLite DB Storage