Java 如何将带有附加功能的意图传递给已经运行的活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18672482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:04:28  来源:igfitidea点击:

How to pass intent with extras to an already running activity

javaandroidandroid-intentandroid-activity

提问by Ayrton Senna

I have a BroadcastReceiverwhich launches a HomeActivitywith some information passed with the extras.

我有一个BroadcastReceiver它启动一个HomeActivity带有附加信息传递的信息。

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

当活动已经在运行并且广播接收器再次被触发并尝试HomeActivity使用新信息启动时会发生什么。活动的OnResume()或是否OnCreate()执行?

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

如果没有,是否有任何其他方式在BroadcastReceiver触发a 时传递/重新加载正在运行的活动?

采纳答案by Cory Roy

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

确保当您从 BroadcastReceiver 启动意图时,您设置了 FLAG_ACTIVITY_SINGLE_TOP 标志。

intent.addFlags (FLAG_ACTIVITY_SINGLE_TOP);

...


class HomeActivity extends Activity {
   ...
   @Override
   protected void onNewIntent(Intent intent) {
      Bundle extras = intent.getExtras();
   }
   ...
}

回答by Amio.io

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

只是扩展 Cory Roy 的答案,您也必须在 AndroidManifest.xml 中定义“SingleTop”。

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

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

似乎扩展android.support.v7.app.ActionBarActivity这个方法不起作用......