The release of Singleton objects and some data in android

Source: Internet
Author: User

The release of Singleton objects and some data in android

It has been some time since I got started with the official android development, and the first version of the project is almost complete. When I was testing, I switched my android project to the background, opened several applications, and switched back to my app. I found an error. After investigation, we found that the data in our singleton object has been released, that is, the int variable value has changed to 0, and the string variable value has changed to null.

My Singleton was like this (example) at the beginning );

 

public class UserInfo {private static UserInfo userInfo = null;private int level;private UserInfo() {      }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 common Singleton class that is used to store frequently used variables. It is normal for me to use normally in various activities and fragment, the following code snippet:

 

If (UserInfo. getInstance (). getLevel () = 3) {// code block}

 


However, when I use my testing machine (the testing machine is MB of memory) for testing, I first switch the android Application to the background and open multiple other apps, such as qq, when we restored our application to the front-end, the code block in the above example did not enter. I found that the value of getLevel () was 0, that is, it was released. I googled some people. It is okay to use a singleton like me on android, while some people will say that their Singleton has been released, that is, the situation is the same as mine. It may be because the memory on the mobile phone is tight, and the data stored in the single-instance object storage is forced to be released.

Next we need to find a solution to this problem.

(1) Use android application, this method originally I want to use, but probably checked, such as this article: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0204/2409.html

It is recommended that you do not use this method, or even use a singleton if it is written in the article ~~ I tried it myself and felt the problem still exists.

(2) onSaveInstanceState () and onRestoreInstanceState (). When this problem occurs, android forcibly removes my current activity, data in my singleton object may be released at the same time. I have indeed executed the onSaveInstanceState () function, and executed onRestoreInstanceState () to re-paint my activity when the switch is back. Then I can repaint my activity in onSaveInstanceState () store my Singleton data, and then assign a value to my data in onRestoreInstanceState. This seems to be okay, but in this case, it's too much for a single instance to use, and I don't feel like a single instance is needed ..

(3) Use SharedPreferences to access data. When assigning values to and retrieving data, use the following code:

 

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;}
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 assigning values to the data in the singleton object, store the data in the preference. When using the data, retrieve the value from the preference and perform corresponding operations, in this case, even if my Singleton is released, I can still retrieve data from the preference. after doing so, I find that my previous bug disappears, all kinds of errors will not be caused by empty data.

However, if you think about this, the singleton mode is slightly redundant here. I can directly access preference. Why should I write it like this? Because the project progress is relatively urgent, I had to use the (3) method to modify my code to ensure that it does not have such a bug, but from my perspective, it is not very good, and the code is redundant. However, the singleton mode is the most commonly used design mode in our work. We often use it at school. Isn't it possible to use Singleton in android? I am skeptical about this.

I wonder if you have encountered the same problem as me. If you have encountered it, how can we solve it? How can I use Singleton mode in android? I hope you can share your thoughts with others who have good methods ~

Related Article

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.