We usually use Sharedpreferences to store some of the needs to be saved locally. But not some data stored in the database. Generally we use it to store some username,password and other data is very convenient, then assume that we want to store information there are 10 of several. We have to write 10 lines of repeated code to put them in. When you take it out, you have to write 10 lines of code to take them out of one piece, so is there any way to save them in a single line of code and take them out of the line? The answer is yes, and this article shows you how to do it through reflection.
1. encapsulate the data we want to save as an entity class Person.java attributes we all use public to decorate
2.Through reflection. Take a property out of the person class let's try to test the property if it has been taken out, we add the following code to the Mainactivity.java for example. Let's take a look at the log printing information we can see that the attributes in the person we've got.
3.Storing objects in a sharedpreferences
4.Remove an object from the Sharedpreferences
5.Let's test it, change the code in Mainacitvity.java. Let's take a look at the Savedperson information printed in log
Source code Download
Android uses reflection to create a universal sharedpreferences that can store no matter what object