【发布时间】:2017-03-01 14:03:00
【问题描述】:
我有一个小部件,其中有说话按钮。在小部件上按下说话按钮时,我正在打开 MainActivity,它在清单中声明为单顶。
以下是说话小部件按钮上的代码
Intent speakIntent = new Intent(context, MainActivity.class);
speakIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
speakIntent.putExtra(Constants.IS_LAUNCH_SPEAK, true);
speakIntent.setAction(ACTION_WIDGET_SPEAK_SCREEN);
PendingIntent configSpeakPendingIntent = PendingIntent.getActivity(
context, Utils.generateRandom(), speakIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(
R.id.btn_speak, configSpeakPendingIntent);
这会打开 google speak 对话框并且工作正常。
但是,如果我按回此对话框并通过在恢复应用程序时再次按回退按钮来最小化应用程序,即使应用程序不是从小部件启动,它也会显示语音对话框
即使活动被破坏,也没有清除意图额外内容我也尝试使用 removeExtra 清除额外内容,但这也不起作用
由于活动在清单中具有单顶启动模式,我检查了 OnNewIntent 是否被调用,但 OnNewIntent 没有被调用 oncreate 在恢复应用程序时被调用
尽管活动被破坏且 removeExtra 不起作用,但从小部件事件调用时,意图附加功能并不清楚
如何解决这个问题。
通过以下通知打开活动时发生同样的事情是代码
Intent intent = new Intent();
intent.putExtra(Constants.SOME_VALUE, value);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, Utils.generateRandom() /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
【问题讨论】:
-
尝试同时使用 PendingIntent.FLAG_ONE_SHOT 和 FLAG_UPDATE_CURRENT
-
所以 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT 一次性标志将确保特定的待处理意图仅使用一次
-
试过了也不行
-
请看这个类似的问题和我的回答:stackoverflow.com/a/19820057/769265
标签: android android-intent android-widget android-pendingintent android-bundle