0

I need to receive a broadcast event inside a broadcast receiver and than pass this information to an activity that is already open. How can I inform the activity from the broadcast receiver without it getting recreated? This causes a total refresh which is not needed.

Now I could receive intent inside a broadcast receiver that is declared within the activity, but I also need to receive the intent when its in background as well, hence the main place I am processing the intents is in a separate broadcast receiver. So I just don't know how to inform the activity that a new intent has arrived without onCreate() getting called and re-init the whole UI.

I think I need the NEW_TASK flag or it won't run.

PS: What are these insane downvotes about. What could be more relevant than how to start an activity from a broadcast receiver in such a way as not to recreate the activity. BTW, I am going to find an answer w/wo you. Why the bitter downvotes? I suspect it is because you know I could use an answer. Well I'll probably be posting an answer to this great question myself quite soon.

Android Developer
  • 523
  • 2
  • 5
  • 17

2 Answers2

2

if you want to recreate this activity do this:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

if don't want to recreate , just do this:

  • while this activity on your task's top,intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  • declare the actvity in the manifest with: android:lauchMode= "singleTask";
Siddharth
  • 9,349
  • 16
  • 86
  • 148
xlwplm
  • 36
  • 1
1

And the answer is .....

declare the Activity in the manifest with

  android:launchMode="singleTop"

Still would like feedback on this answer as I notice that in onResume() the intent does not seem to carry over the values as it did before. So I cannot pull out values that I set on the intent inside the broadcast receiver ...

Update: In order to get the values from the receiver you may need to do the following inside the Activity:

             @Override 
     public void onNewIntent(Intent intent)
     {
         setIntent(intent);
     }
Android Developer
  • 523
  • 2
  • 5
  • 17