How to pass intent with extras to an already runni

2019-03-17 13:26发布

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?

2条回答
闹够了就滚
2楼-- · 2019-03-17 13:54

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();
   }
   ...
}
查看更多
劳资没心,怎么记你
3楼-- · 2019-03-17 14:01

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

查看更多
登录 后发表回答