Reputation: 15718
I am using Kotlin from quite some time now, but I could not able to achieve not-null types for all the properties in Kotlin.
Please take a look at below code, there are some scenarios where I have to use null types. I know I can use lateinit
but in some scenarios, it does not fit. How can I avoid null in my code?
If anyone can re-write the code with without null types or correct my mistakes, it is more than enough for me to understand everything.
class MusicService : Service(), PlaybackManager.PlaybackServiceCallback {
private val mDelayedStopHandler = DelayedStopHandler(this)
private val eventBus = EventBus.getDefault()
//How to avoid nullable types
private var mMediaNotificationManager: MediaNotificationManager? = null
private var mSession: MediaSessionCompat? = null
var mSessionToken: MediaSessionCompat.Token? = null
var mPlaybackManager: PlaybackManager? = null
var mTransportControls: MediaControllerCompat.TransportControls? = null
override fun onCreate() {
Timber.d("onCreate")
super.onCreate()
//Init MediaSessionCompat and TransportControls
mSession = MediaSessionCompat(this, "MusicService")
mSessionToken = mSession?.sessionToken
mSession?.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
mTransportControls = MediaControllerCompat(this, mSession).transportControls
//EventBus Reg
eventBus.reg(this)
eventBus.post(GetAllMediaEventRequest())
}
@Subscribe
fun onGetAllMediaEventResponse(event: GetAllMediaEventResponse) {
Timber.d("GetAllMediaEventResponse event.status = ", event.status)
//init PlaybackManager
mPlaybackManager = PlaybackManager(mPlayback = LocalPlayer(this),
mMediaData = event.mediaItems,
mServiceCallback = this)
mSession?.setCallback(mPlaybackManager!!.mMediaSessionCallback)
//Init Notification
try {
mMediaNotificationManager = MediaNotificationManager(this)
} catch (e: RemoteException) {
throw IllegalStateException("Could not create a MediaNotificationManager", e)
}
}
}
Update:
Thanks for all the response which I've got. After a bit of research, I made all properties not nullable. Please check my code and correct me if any mistakes.
class MusicService : Service(), PlaybackManager.PlaybackServiceCallback {
//NotNull
private val mDelayedStopHandler = DelayedStopHandler(this)
private val eventBus = EventBus.getDefault()
//Lateinit
lateinit var mSessionToken: MediaSessionCompat.Token
lateinit var mTransportControls: MediaControllerCompat.TransportControls
//Lazy
private val mSession: MediaSessionCompat by lazy { MediaSessionCompat(this, "MusicService") }
private val mMediaNotificationManager: MediaNotificationManager by lazy {
try {
MediaNotificationManager(this)
} catch (e: RemoteException) {
throw IllegalStateException("Could not create a MediaNotificationManager", e)
}
}
val mPlaybackManager: PlaybackManager by lazy {
PlaybackManager(mPlayback = LocalPlayer(this), mServiceCallback = this)
}
override fun onCreate() {
LogHelper.d(TAG, "onCreate")
super.onCreate()
//Init MediaSessionCompat and TransportControls
mSessionToken = mSession.sessionToken
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
mTransportControls = MediaControllerCompat(this, mSession).transportControls
mSession.setCallback(mPlaybackManager.mMediaSessionCallback)
//EventBus Reg
eventBus.reg(this)
eventBus.post(GetAllMediaEventRequest())
}
@Subscribe
fun onGetAllMediaEventResponse(event: GetAllMediaEventResponse) {
Timber.d("GetAllMediaEventResponse event.status = ", event.status)
mPlaybackManager.mMediaData = event.mediaItems
}
}
Upvotes: 0
Views: 356
Reputation: 6569
I guess you might need some
a?.let {
println(it)
// if `a` isn't null, the code will reach here
// and `it` will hold the value of `a`
// you can do a lot of things here without checking if it is null
}
Upvotes: 1