It's been a while since the first version of the project has been in contact with the official Android development for some time. Once in the test, I cut my Android project into the background, and opened several applications and then re-cut back to your app and found an error. After troubleshooting, it is found that the data in the Singleton object is freed, that is, the value of the int variable becomes the value of the 0,string variable and becomes null.
My singleton is like this at the outset (for example);
public class UserInfo {private static UserInfo UserInfo = null;private int level;private UserInfo () { <span style= " White-space:pre "></span>}public static UserInfo getinstance () {if (null = = UserInfo) {UserInfo = new UserInfo (); }return UserInfo;} public void SetLevel (Int. level) {this.level = level;} public int Getlevel () {return level;}}
This should be a more common singleton class, used to store our frequently used variables, usually my use is not a problem, in each activity,fragment can be used normally, such as the following code snippet:
if (Userinfo.getinstance (). Getlevel () = = 3) {//Our code block}
However, when I use my test machine (test machine is 512m memory) to test, first put the Android app into the background, open a number of other apps such as QQ, and then restore our application to the foreground, The code block of the above example is not actually entered, I find that the value of Getlevel () is already 0, which is released by print debugging. I google for a while, some people on Android using a singleton like me is not a problem, and some people will say that their own singleton was released, that is, the same as my situation. It is possible that the data stored by our Singleton object is forced to be freed due to a tight memory on the phone.
Here's how to find out how to solve this problem.
(1) Using Android application, this method originally I wanted to use, but probably checked, such as this article: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/ 2015/0204/2409.html
Found it is best not to use this method, even the article did not use a single example ~ ~ ~ I tried it myself, feeling the problem still exists.
(2) Onsaveinstancestate () and Onrestoreinstancestate (), when I had this problem, Android was forced to remove my current activity, possibly releasing the data from my singleton object at the same time. I did execute the onsaveinstancestate () function and onrestoreinstancestate () redraw my activity at the time of the cut back, so I can do it in Onsaveinstancestate () Store my singleton data and re-assign my data in Onrestoreinstancestate (). This seems to be possible, but, in this case, the singleton used to be too much of that, the feeling does not need a single case.
(3) using Sharedpreferences to access the data, when we assign data and take out the data, we might as well use the following code:
<span style= "White-space:pre" ></span>public void setLevel (int level, context context) { Savesharedpreferences (Context, "level", level+ ""); this.level = level;} public int Getlevel (context context) {level = Integer.parseint (utils.readsharedpreferences (Context, "level"); return level;}
<span style= "White-space:pre" ></span>public static void Savesharedpreferences (context context, String Name, String data) {sharedpreferences sharedpreferences = context.getsharedpreferences ("School_user_info", Context.mode_private); Sharedpreferences.editor Editor = Sharedpreferences.edit (); editor.putstring (name, data); Editor.commit ();} public static string Readsharedpreferences (context context, string name) {Sharedpreferences sharedpreferences = Context.getsharedpreferences ("School_user_info", context.mode_multi_process); return sharedpreferences.getstring ( Name, "");}
When I assign a value to the data in a singleton object, we put this data in preference, use it, then take my value out of the preference and do the corresponding operation, so that even when my singleton is released, I can still extract the data from the preference. After doing this, I found that my previous bug had disappeared, and that it would not cause all sorts of errors because the data was empty.
However, think carefully, found that the singleton mode is used here slightly redundant, I directly access the preference on it, why do you want to write it? Because the project is more urgent, I had to use the (3) method to modify my code to ensure that it does not have such a bug, but in my current view, it is not very good, feeling caused by the code superfluous. But the singleton mode is the most commonly used design pattern in our work, we often use it when we are in school, can't we use the single example in Android? I'm skeptical about that.
Do not know if you have encountered and I have the same problem, if encountered, and how to solve it? How do I use singleton mode in Android? Hope to have a good way of students can share their views ~
Problems with the release of Singleton objects and certain data in Android