【问题标题】:Intent not picked up by singleTop ActivitysingleTop Activity 未获取 Intent
【发布时间】:2012-10-12 14:17:56
【问题描述】:

我有一个定义为 singleTop 的 Activity,因此只有一个实例存在。

   <activity
        android:name=".MyMainActivity"
        android:label="@string/app_name" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize" 
        android:launchMode="singleTop" android:multiprocess="true">

我用一些数据设置了一个通知意图,并将其包含在一个 PendingIntent 中,并将其发送到通知管理器。

Intent notificationIntent = new Intent(context, MyMainActivity.class); 
notificationIntent.setAction(MyMainActivity.CustomInternalMessageAction);
notificationIntent.putExtra(MyMainActivity.ReceiveTestMessage, rawMessageText);

...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotificationManager.notify(number, notification);  

如果 Activity 没有运行,Intent 会按预期通过 onCreate() 启动 Activity。

如果活动正在运行,但处于停止状态/不在前台(如单击主页按钮时),并且单击了通知,则按预期调用我的活动的 onNewIntent()。

但是,如果 Activity 在前台时已被调用,则不会调用 onNewIntent()。

如何设置此通知,以便在我的 singleTop Activity 处于暂停/停止状态时发送意图(并且在我提到的其他情况下仍然有效)。我想在我的 notificationIntent 对象上设置一个标志,但是对于 singleTop 活动的情况,标志的功能并不是很清楚。

【问题讨论】:

  • 在这种情况下是否会调用其他任何 on 方法?
  • 不,不是基于我的日志记录。 onCreate()、销毁、恢复、暂停、onSaveInst、onRestoreInst
  • 这似乎只是某些设备上的问题。我正在处理类似的情况,我有一台运行良好的设备,另一台没有收到新的 Intent。在我的意图上设置 CLEAR_TOP 标志修复了它在有问题的设备上。

标签: android android-intent notifications


【解决方案1】:

如果您的应用程序由多个 Activity 组成,那么如果您想在该 Activity 处于活动状态时接收 onNewIntent()call,则还需要将剩余的 Activity 定义为 singleTop

假设主要活动是A,并被定义为singleTop,然后从那里开始活动B,而不是定义为singleTop。如果现在您选择调用您的应用程序的通知,应用程序将在活动 B 上启动,但不会调用 onNewIntent()

您还可以覆盖此行为,添加标志Intent.FLAG_ACTIVITY_CLEAR_TOP,从堆栈中删除活动B,活动A 将接收onNewIntent() 上的调用。

【讨论】:

  • 就我而言,我的主要活动 A 是我希望通过通知显示的内容。因此,如果通知没有在我的活动 A 上调用 onNewIntent(),则活动 B 不在堆栈中。
  • 添加 Intent.FLAG_ACTIVITY_CLEAR_TOP 在我的情况下有效。 onNewIntent() 被调用。
猜你喜欢
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-01
相关资源
最近更新 更多