Reputation: 947
I need to implement a singleton MyClass
in kotlin.
Requirements:
SuperClass
and I need to call the constructor of Superclass
MyClass
and need the context to call the constructor of Superclass
.MyClass
is a singleton.Java equivalent:
class MyClass extends SuperClass
{
// instance variable
// getInstance() method
MyClass(Context context)
{
super(context);
}
}
I tried to solve this with an object
but didn't get it working.
Is there a way to get it working with an object or do I have to use a companion object
?
Upvotes: 1
Views: 5223
Reputation: 38223
Consider the following super class:
open class MySuperClass(val context: Context) {...}
Since Kotlin objects only have an empty constructor, you'd need a structure similar to the following:
// Private constructor is only accessible within the class.
class MySingleton private constructor(context: Context) : MySuperClass(context) {
companion object {
lateinit var INSTANCE: MySingleton
// Instance setter is only accessible from within the class.
private set
// Custom init function is called from outside and replaces
// THE WHOLE SINGLETON with a new instance
// to avoid internal dependencies on the old context.
fun init(context: Context) {
INSTANCE = MySingleton(context.applicationContext)
}
}
// Lazily initialized field dependent on a Context instance.
val prefs by lazy { PreferenceManager.getDefaultSharedPreferences(context) }
}
You need to call init(context)
once before using your singleton class and the Application
is a great place to do that. This will also create a new instance of your singleton each time Instant Run loads a new Application
object so you always end up with the most up-to-date application context.
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// Eagerly initialized singleton.
MySingleton.init(this)
}
}
Notes:
class
instead of object
and manage current instance yourself. You also have to do this if you need to pass parameters to super class of your singleton.getInstance(context)
it's a great idea to lazily the heavy objects within your singleton object.Upvotes: 2
Reputation: 27971
You don't need any objects or companion objects to achieve this. This is the syntax for calling the constructor of a superclass in Kotlin:
class MyClass(context: Context) : SuperClass(context)
Upvotes: 0