0

I want to send simple sata from an activity to another. Both are active at the time and I have registered LocalBroadcastManager. the sending part work but the receiving activity doesn't get anything. BroadcastManager sending data part of the code :

myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LocalBroadcastManager broadcaster = LocalBroadcastManager.getInstance(SendingDataPageActivity.this);
            Intent broadcastIntent = new Intent(BROADCAST_INTENT);
            broadcastIntent.putExtra("test, "test");
            broadcastIntent.setAction("test, "test");
            broadcaster.sendBroadcast(broadcastIntent);
            Log.d(TAG, "Broadcast sent..."); });

And For receiver:

public class RecievingDataPageActivity extends AppCompatActivity{
BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "local broadcaster received...");}};

I have registered it in OnCreate:

LocalBroadcastManager.getInstance(this).registerReceiver((receiver),
            new IntentFilter(BROADCAST_INTENT));

But still doesn't receive anything. Any help?

Ali Has
  • 598
  • 1
  • 8
  • 24
  • Ok I finally found the problem: I had unregistered the BroadcastReceiver in onStop() which caused ReceicerActivity to stop receiving it after going to a second activity especially the sender. Thank you all. – Ali Has Jul 01 '18 at 11:49

2 Answers2

0

Remove this line and try again.

broadcastIntent.setAction("test, "test");

Baskar T
  • 1
  • 1
0

You are setting your action in the Intent constructor:

 Intent broadcastIntent = new Intent(BROADCAST_INTENT);

But 2 lines after, you are overriding it:

  broadcastIntent.setAction("test", "test");

You can only have one action so Intent gets lost.

Facundo Larrosa
  • 3,349
  • 2
  • 14
  • 22