问题
I know about Singleton, but I can't use it in an Android project. I am a beginner in Android. Please tell me how & where we can use Singleton in an Android project for Large Data. I have used it for simple values.
回答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;
}
}
回答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 info on the singleton pattern, read http://en.wikipedia.org/wiki/Singleton_pattern
For more info on SQLite in android read: http://developer.android.com/guide/topics/data/data-storage.html#db
来源:https://stackoverflow.com/questions/12585720/how-to-use-the-singleton-pattern-in-an-android-project