0

Context: No receiver is declared in the manifest since I am not declaring a new receiver.

I am a bit confused about why the receiver in MainActivity does not receieve the broadcast sent from the recycler adapter.

RecyclerAdapter

        holder.checkBox.setOnClickListener {view ->
            item.completed = holder.checkBox.isChecked
            Log.i("wow", "is checked: ${holder.checkBox.isChecked}")
            val intent = Intent().apply {
                addCategory(Intent.CATEGORY_DEFAULT)
                setAction(changeCompletedForDeck)
                putExtra(changeCompletedForDeckItemID, item)
            }
            LocalBroadcastManager.getInstance(view.context).sendBroadcast(intent)

MainActivity

 private lateinit var broadcastReceiver: BroadcastReceiver

          broadcastReceiver = object: BroadcastReceiver() {
           override fun onReceive(context: Context?, intent: Intent?) {
               //get deck, if deck != null then update the checkmark response
               if (intent?.action == DeckRecyclerAdapter.changeCompletedForDeck) {
                   val deck = intent?.extras?.getParcelable<Deck>(DeckRecyclerAdapter.changeCompletedForDeckItemID)
                   Log.i("wow", "${deck?.title}")
                   deck?.let { deck ->
                       globalViewModel.update(deck)
                   }
               }
           }
       }
val filter = IntentFilter(DeckRecyclerAdapter.changeCompletedForDeck)
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, filter)


    //Destroy the BroadcastReceiver
    override fun onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
        super.onDestroy()
    }

PandaPlaysAll
  • 927
  • 2
  • 9
  • 15
  • I don't see `registerReceiver` in your `MainActivity` where did you registered it ? – ADM Sep 17 '20 at 02:46
  • @ADM Updated, sorry forgot to link it. Is my issue related to not having anything in the Android Manifest file? – PandaPlaysAll Sep 17 '20 at 03:24
  • No its not related to manifest . Chekout the answer below See if you understand what i am implying. If you need code sample i will add it too . – ADM Sep 17 '20 at 04:11

1 Answers1

1

Your problem is Intent action . See at the time of register you have not provided any action so receiver will not be identified by the system. You can define a specific action with IntentFilter and use the same action during register and sendBroadcast.

To identify different conditions you can do two things.

  1. you can set data in Bundle and validate the bundle value inside onReceive()
  2. you can also add multiple actions to IntentFilter and validate the action inside onReceive() See this.

So with the first way have a constant action in MainActivity:-

companion object{
    const val BROADCAST_ACTION="LIST_CHECK_ACTION"
}


LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter(BROADCAST_ACTION)).

Then for sending broadcast use the code below addCategory(Intent.CATEGORY_DEFAULT) is not required:-

val intent = Intent().apply {
        action = MainAcvity.BROADCAST_ACTION
        putExtra("item", item)
    }
    LocalBroadcastManager.getInstance(view.context).sendBroadcast(intent)

PS- However i don't think you should be using a Broadcastreceiver just to provide a callback from Adapter its purpose is more than that. You should be using a callback listener for it . Since RecyclerView.Adapter will binds to a UI component a callback interface will be fine . I think a broadcastReceiver is overkill in this usecase .

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Can you please clarify, I am getting the gist of what you are saying. But all I need is the intent from recycler view as all I require is passing a single parcelable object. I am a bit confused with the IntentFilter, as they are supposedly matching the action. But an intent also has an action etc. Also I only require one action anyway. Thus, I have initialised in the constructor – PandaPlaysAll Sep 17 '20 at 04:32
  • Just one last thing as I now understand what prevented the code from working. (I'll mark this response as the answer). Okay, so following what you had in the code snippet. The thing that prevented the code from executing was addCategory. How does that play a part and prevent the broadcaster from working? is it used for multiple possibilities? – PandaPlaysAll Sep 17 '20 at 04:57
  • [This](https://developer.android.com/reference/android/content/Intent#CATEGORY_DEFAULT) should answer your question. – ADM Sep 17 '20 at 05:02