Broadcast Receiver in kotlin. You can do it in the following way. Create a broadcast receiver object in your activity class.
1 2 3 4 5 6 7 8 9 10 | val broadCastReceiver = object : BroadcastReceiver() { override fun onReceive(contxt: Context?, intent: Intent?) { when (intent?.action) { BROADCAST_DEFAULT_ALBUM_CHANGED -> handleAlbumChanged() BROADCAST_CHANGE_TYPE_CHANGED -> handleChangeTypeChanged() } } } |
Register broadcast receiver in onCreate() function of your activity
1 2 | LocalBroadcastManager.getInstance(this) .registerReceiver(broadCastReceiver, IntentFilter(BROADCAST_DEFAULT_ALBUM_CHANGED)) |
Unregister it in ondestroy function of your activity
1 2 | LocalBroadcastManager.getInstance(this) .unregisterReceiver(broadCastReceiver) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.