Correct writing of Android SharedPreferences storage
SharedPreferences features
Even Android Xiaobai knows the usage of SharedPreferences, which is the easiest way to save data, but if it is not handled properly, later maintenance will be a huge pit. So how to handle SharedPreferences to facilitate maintenance. Let's start with its characteristics.
The specific implementation of SharedPreferences is not in the public API, and the creation method can only be obtained through context. We won't study the specific implementation. The object obtained each time through log printing is the same. You can know that different objects will be obtained each time you edit ().
Hard to maintain SharedPreferences
As mentioned above, the method of saving and reading SharedPreferences is very simple, which is why it is difficult to maintain. The problem may not be found due to the wrong key. Of course, we usually write the key as a static constant. However, with the increase of the project, it may be necessary to modify user information in many places. Put () one at a time. In addition to writing, it is very inconvenient to add or reduce fields.
That's what SharedPreferences should say
If you can keep the original writing method and standardize the method, it's best. Look at the interface methods of SharedPreferences. It's easy to write a wrapper class to encapsulate it.
The next use is simple
Call is convenient for extension and retains the original method.
Thank you for reading, hope to help you, thank you for your support to this site!