Reputation: 4933
I want to start an Intent service in kotlin but my service has a parameter i.e. a presenter object. Now we need a default no arg constructor for intent service. How can I achieve that in kotlin?
Is there a way I can overload constructor? I don't think I can pass a default value for the presenter in the constructor
class SongIdentifyService(discoverPresenter : DiscoverPresenter) : IACRCloudListener , IntentService("SongIdentifyService") {
private val callback : SongIdentificationCallback = discoverPresenter
private val mClient : ACRCloudClient by lazy(LazyThreadSafetyMode.NONE) { ACRCloudClient() }
private val mConfig : ACRCloudConfig by lazy(LazyThreadSafetyMode.NONE) { ACRCloudConfig() }
private var initState : Boolean = false
private var mProcessing : Boolean = false
override fun onHandleIntent(intent: Intent?) {
setUpConfig()
addConfigToClient()
startIdentification(callback)
}
}
Upvotes: 2
Views: 1024
Reputation: 82087
You can have two secondary constructors like this (simplified):
class SongIdentifyService {
constructor(discoverPresenter: DiscoverPresenter)
constructor()
}
Or make discoverPresenter nullable and give it a default value:
class SongIdentifyService(discoverPresenter: DiscoverPresenter? = null)
Upvotes: 2