Android on single object and some data issues are released

Source: Internet
Author: User

Formal contact with Android we have been developing for some time, and the first version of the project will end soon.

When there is a test of its own. Have Android backstage. At the same time, several apps are opened and then cut back to your app. 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 starts with this (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 relatively common singleton class. Used to store variables that we often use. Usually my use is not a problem. In each activity. Fragment can be used normally, as in 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 example above actually did not enter, I found through the print debugging Getlevel () value is already 0, that is released.

I have Google for a while, some people on Android using a single example like me is no problem. Others would say that their singleton was released. That's the same as my situation.

It is possible that the data stored by our Singleton object is forced to be freed because of the tight memory of the phone.

Here's how to find a solution to this problem.

(1) Use Android application. This method originally I wanted to use, but probably checked. For example, this article: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0204/2409.html

Found it is best not to use such a 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. The data in my singleton object may have been released at the same time.

I did run the Onsaveinstancestate () function, too. At the same time, when I cut back and run onrestoreinstancestate () to redraw my activity, I was able to store my singleton data in Onsaveinstancestate (), Then I assign a value to my data again in Onrestoreinstancestate (). It seems to be possible, but. This kind of words, the single case used to be too that what, feel do not need single case.

(3) The operation of accessing data using Sharedpreferences. When we are assigning data and fetching data, it is best to 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 you assign a value to the data in a singleton object. Put this data in preference, use, and then remove my value from the preference, do the corresponding operation, so to do, even when my singleton was released. I am still able to extract 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.

But. Think carefully. If we find this, the singleton mode is slightly redundant here, I can access the preference directly. Why do you have to write like that? Because of the urgency of the project, I just use (3) method changed my code to ensure that it does not have this bug. But this is what I look at now. is not very good, feeling caused by the superfluous code. However, the singleton pattern is the most frequently used design pattern in our work. In high school, we often use it, it is on Android when you can not do so using a single case? I am sceptical.

I do not know, I have not encountered the same problem, assuming satisfaction, and how to solve it? How to Androidsingleton mode with it? I hope there is a way to develop students ' good ability to share their views ~

Android on single object and some data issues are released

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.