Method 1: Flattened Objects with Field Keys
The idea here is that, although SharedPreferences only stores primitive types, an object construct is ultimately a “bag” of primitives. So we can decompose an object into a set of primitives and store each one of those individually with a unique key. However we do need to keep track of which saved field belongs to which object if we want to reconstruct our objects from storage.
So before writing the CRUD methods on a given User object in our UserPrefs class, we need to define unique keys on each User class member. For convenience we can write a method to ensure key uniqueness across the board:
Notice how getFieldKey() gives us a unique identifier per field name and per user.