29

I have a BroadcastReceiver which launches a HomeActivity with some information passed with the extras.

What happens when the activity is already running and the broadcast receiver gets triggered again which tries to launch the HomeActivity with new info. Does the OnResume() or OnCreate() of the activity execute?

If not, is there any other way of passing/reloading a running activity when a BroadcastReceiver is triggered?

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
Ayrton Senna
  • 3,735
  • 5
  • 34
  • 52
  • 2
    Perhaps this may answer your question: http://stackoverflow.com/questions/10143222/sending-data-to-another-activity-that-may-be-started (you have to override onNewIntent() ). I'm not sure though. – u3l Sep 07 '13 at 10:54
  • 1
    You need an `Interface`. Define a callback. – Simon Sep 07 '13 at 11:22

3 Answers3

49

Make sure when you are launching the intent from the BroadcastReceiver you set the FLAG_ACTIVITY_SINGLE_TOP flag.

intent.addFlags (FLAG_ACTIVITY_SINGLE_TOP);

...


class HomeActivity extends Activity {
   ...
   @Override
   protected void onNewIntent(Intent intent) {
      Bundle extras = intent.getExtras();
   }
   ...
}
Cory Roy
  • 5,379
  • 2
  • 28
  • 47
5

Just extending Cory Roy's answer you have to define "SingleTop" in AndroidManifest.xml too.

<activity
        android:name="MainActivity"            
        android:launchMode="singleTop"

It seems that extending android.support.v7.app.ActionBarActivity this method does not work...

Amio.io
  • 20,677
  • 15
  • 82
  • 117
  • 2
    this does not seem to be necessary! This means that the default launch mode is 'singleTop'. you might not want that and only for notifications – Boy Apr 07 '17 at 13:35
  • 1
    Worth noting that launchMode only needs to be set either in the manifest or via addFlags() from the calling intent, not both. – Cody Jan 04 '18 at 01:09
0

Forget starting activities from a Broadcastreceiver, google added since Android 12 a trampoline prevention. See docs: https://developer.android.com/about/versions/12/behavior-changes-12