【问题标题】:Prevent new activity instance after clicking on notification单击通知后阻止新的活动实例
【发布时间】:2011-03-07 21:12:34
【问题描述】:

应用程序(不需要的)行为 -

  1. 应用程序启动,一些文本被放入文本框中,并通过按钮操作创建通知。
  2. 用户“点击”主页按钮,应用程序被“最小化”,通知栏可用
  3. 用户选择通知,应用程序被“最大化”

但是 - 不是原始实例,而是启动新实例(例如,在最新实例中缺少原始文本;当最新实例关闭时,仍然有原始实例与原始文本)。

通知方法的代码

Context context = getApplicationContext();
    CharSequence contentTitle = "someText1";
    CharSequence contentText = "someText2";
    Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
    notifyIntent.setClass(getApplicationContext(), RadioStream.class);
    PendingIntent intent = 
       PendingIntent.getActivity(RadioStream.this, 0, notifyIntent, 0);

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

我在清单 xml 文件中也有以下标记

android:launchMode="singleTask"

但还是一样... 主要问题是应用程序的双重/三重初始化,我知道还有其他方法可以保留恢复的应用程序中的值。 此外,应用程序需要在后台保持运行,因为主要功能是互联网广播的流媒体。

代码中缺少什么? 我缺少哪些信息来解决问题?

谢谢!

戴夫

【问题讨论】:

标签: android android-activity instance notifications


【解决方案1】:

您所说的“应用程序”很可能是一个活动。为避免在将其放在前面时重新装箱,请使用

android:launchMode="singleTop"

要让一些代码在后台运行,你需要将其实现为Service

【讨论】:

  • 没有帮助:-/重新服务-明白,我放弃我目前的活动概念谢谢!
【解决方案2】:

另一种选择是在您的Intent 上使用setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)

【讨论】:

  • 我将在今天晚些时候在http://github.com/commonsguy/cw-andtutorials(查看20-Notifications)上发布演示此技术的代码。
  • 如果您还为清单中的给定 Activity 设置了 android:launchMode="singleTop" ,那么这可以解决问题。
  • 我在 CommonsWare 的解决方案中添加了 Warpzit 的评论,它对我有用。谢谢。
  • 认为这个@CommonsWare 的答案应该是正确的答案。另外,我发现只有使用 Intent.FLAG_ACTIVITY_SINGLE_TOP 标志对我有用。包括Intent.FLAG_ACTIVITY_SINGLE_TOP 也会在 Activity 开始之前导致短暂的空白屏幕....不太光滑。
【解决方案3】:

将此标志添加到您的活动属性中。

android:launchMode="singleInstance"

【讨论】:

    【解决方案4】:

    您可以在 PendingIntent 上设置 PendingIntent.FLAG_CANCEL_CURRENT:

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
    

    如果activity已经存在,新的会取消,不会更新旧的! 这对我有用!

    【讨论】:

      【解决方案5】:

      作为服务重新实现 - 一切都按预期工作

      【讨论】:

        【解决方案6】:

        这将使应用返回到它的 MainActivity,即使它已经打开了子活动。子活动也被清除。

        AndroidManifest:

        android:launchMode="singleTop"
        

        代码:

        val intent = Intent(context, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
        val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
        

        【讨论】:

          【解决方案7】:

          我使用 GCM 示例作为个人项目的基本代码,我遇到了上面描述的相同问题。 GCM 示例:https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main/java/gcm/play

          我通过结合答案找到了解决方案:

          1) 来自@ognian 的回答,在应用程序部分添加到 App Manifest:

          android:launchMode="singleTop"

          <application
              android:allowBackup="true"
              android:icon="@mipmap/logo"
              android:label="@string/app_name"
              android:supportsRtl="true"
              android:theme="@style/AppTheme"
              **android:launchMode="singleInstance">**
          

          2) 从@CommonsWare 通过设置 setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)

          private void sendNotification(String message) {
              Intent intent = new Intent(this, MainActivity.class);
              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
              PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                      PendingIntent.FLAG_ONE_SHOT);
          

          【讨论】:

            【解决方案8】:

            使用以下组合对我有用。

            1) 在 Manifest 文件中添加启动模式 android:launchMode="singleInstance"

            2) 在创建意图时设置 setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)。

            intent = new Intent(this, StartUpActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);

            【讨论】:

              【解决方案9】:

              而不是:

              notifyIntent.setClass(getApplicationContext(), RadioStream.class);
              PendingIntent intent =   PendingIntent.getActivity(RadioStream.this, 0, notifyIntent, 0);
              

              只需使用这些行,它会帮助你,

              notifyIntent = new Intent();
              PendingIntent intent= PendingIntent.getActivity(RadioStream.this, 0, notifyIntent, 0);
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2015-01-27
                • 1970-01-01
                • 2020-05-29
                • 2015-08-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多