0

I am trying to understand a Broadcast reciever, Here I have tried registering Reciever programatically so that i can get message from MyReciever class to perticular activity through interface call back, It worked fine. But now i have commented my programatic registering of reciever and I have registered reciever in menifest file.I am recieving the message in MyReciever's onRecieve() method as expected, the problem is how to render that message to perticula activity? In this case my listener will be null i cant use call backs here as per my knowledge (Correct me if i am wrong). How can I achieve this?

Note: Here i am sending two BroadCasts(when on Back pressed), I need to render that or get that message in perticular activity say when he clicks on the back button in SecondActivity then in MainActivity i should get that message, if he clicks back button in fouth activity then i should get that message in ThirdActivity(Reciever registered in Menifest file not programatically).

Complete Code Link: http://pastebin.com/eTwxMcHh , Your help Highly appreciated.

Naveen Shriyan
  • 1,266
  • 1
  • 9
  • 15

2 Answers2

0

Instead of using startActivity use startActivityForResult for starting Second activity from MainActivity.

MainActivity mButton.setOnClickListener code change:

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 9999); //changed startActivity to startActivityForResult.

In Second activity onBackPressed() do:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent();
    intent.setAction("com.navi.test");
    intent.putExtra("INFORMATION","SENT FROM SECOND ACTIVITY");
    //sendBroadcast(intent);
    setResult(Activity.RESULT_OK, intent);
    finish();
}

So instead of sendBroadcast(), you can setResult and intent, and in your MainActivity implement onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && resultCode == 9999) {
        //Read your values from data and implement your action.
    }
}

So this way you will not require any Broadcast receiver, instead you are sending result back to previous activity by setting RESULT_OK and intent with required result values.

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • hey thank you for ur reply, This i can do but i am trying to understand Broadcast Reciever here, is that possible to get the same result using broadcast reciever? i can do it registering reciever programmatically bec i can get the call back to perticular activity but globally registering reciever i.e in menifest file i am facing problem bec i am not able to get the call back bec the listener will be null at that time..:( – Naveen Shriyan Jun 03 '16 at 05:55
0

You can achieve using broadcast receiver but still you have to pass intent to your activity for example just override in your activities.

but don't forgot to add intent.addFlags (FLAG_ACTIVITY_SINGLE_TOP);

@Override
   protected void onNewIntent(Intent intent) {
      ... use intent info to do your stuff
   }
  • after search bit found this answer its same http://stackoverflow.com/questions/18672482/how-to-pass-intent-with-extras-to-an-already-running-activity –  Jun 03 '16 at 07:01