'How can I use the singleton pattern in an Android project? [closed]
I know about singleton, but I can't use it in an Android project. I am a beginner in Android.
How and where can we use singleton in an Android project for large data? I have used it for simple values.
Solution 1:[1]
Singleton in Android is the same as singleton in Java:
A basic singleton class example:
public class AppManager
{
private static AppManager _instance;
private AppManager()
{
}
public synchronized static AppManager getInstance()
{
if (_instance == null)
{
_instance = new AppManager();
}
return _instance;
}
}
Solution 2:[2]
- For "large data" use a database. Android gives you SQLite.
- Of course you can use singletons in Android. What makes you think you cannot?
For more information on the singleton pattern, read Singleton pattern.
For more information on SQLite in Android, read: Data and file storage overview.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Peter Mortensen |
Solution 2 | Peter Mortensen |