我是新android开发和AP preciate任何帮助。我有一个要访问一个Java类的多个机器人的活动。此类已同步的getter和setter方法,但我有使整个活动这个类的一个实例的问题。有什么办法可以轻松地做到这一点?
I am new to android development and appreciate any help. I have multiple android activities that have to access one java class. This class has synchronized getters and setters, but I'm having problems making a single instance of this class across the activities. Is there any way to easily do this?
您需要的是一个'单身' 模式:
public final class Singleton {
private final static Singleton ourInstance = new Singleton();
public static Singleton getInstance() {
return ourInstance;
}
// Contructor is private, so it won't be possible
// to create instance of this class from outside of it.
private Singleton() {
}
}
现在在你的子类,只需使用:
Now in your child classes, just use:
Singleton.getInstance()
要访问一个,这个类的一个对象。
to access one, single object of this class.