Ayrton Senna
Ayrton Senna

Reputation: 3835

How to pass intent with extras to an already running activity

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?

Upvotes: 30

Views: 15729

Answers (3)

Kirill Häuptli
Kirill Häuptli

Reputation: 41

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

Upvotes: 0

Amio.io
Amio.io

Reputation: 21575

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...

Upvotes: 6

Cory Roy
Cory Roy

Reputation: 5609

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();
   }
   ...
}

Upvotes: 52

Related Questions