android - 通知点击 - 如果应用程序关闭打开 Activity ,如果应用程序打开显示当前 Activity

标签 android android-activity notifications android-pendingintent

<分区>

我有一个持续通知的服务。在应用程序本身中,我有两个 Activity - HomeActivitySettingsActivity

目前我所拥有的是->点击通知时:

  1. 如果应用已关闭 -> 打开 HomeActivity

  2. 如果当前显示的 Activity 是 HomeActivity,则将其置于最前面而不创建新的 Activity。

代码:

resultIntent = new Intent(context, HomeActivity.class);
resultPendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = mBuilder
                .setSmallIcon(notificationData.getImageSrc())  // the status icon
                .setTicker("HealthChecker")  // the status text
                .setWhen(System.currentTimeMillis())  // the time stamp
                .setContentTitle("HealthChecker")  // the label of the entry
                .setContentText(notificationData.getText())  // the contents of the entry
                .setOngoing(true).setContentIntent(resultPendingIntent)
                .build();

list :我添加了`android:launchMode="singleTop":

<activity
            android:name=".ui.HomeActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme.NoActionBar"></activity>

我遇到的问题是:

当我在 SettingsActivity 中并单击通知时,它会打开一个新的 HomeActivity 实例。

我想要的是当应用程序在任一 Activity 中打开并单击通知时 -> 显示当前 Activity ,如果应用程序关闭并单击通知,则打开HomeActivity 的新实例。

最佳答案

when I am in the SettingsActivity and I click the notification, it opens a new instance of HomeActivity.

因为这就是 singleTop 的用途:

However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.

这是你的情况,因为你在上面的是 SettingsActivity

What I want is when the app is opened in either activity and notification is clicked -> show current activity, and if app is closed and notification is clicked, open new instance of HomeActivity

那么您的通知可能应该将 Intent 发送到第三个 Activity (没有 ui),然后该 Activity 将进一步重定向并 finish()。根据您的 targetSdk 是什么,您可能想要使用 ActivityLifecycleCallbacks确定您的应用何时位于最前面。

关于android - 通知点击 - 如果应用程序关闭打开 Activity ,如果应用程序打开显示当前 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37549887/

相关文章:

java - 如何从指纹扫描仪获取输入并保存

android - AppsFlyer 集成在 Android 中启用/禁用跟踪未按预期工作?

android - 通过 Android Intent 将链接发送到 Whatsapp

android - 在运行时更改显示语言并重新创建所有 Activity

android - 是否可以取消所有具有特定标签的通知?

android - 与 adb 的连接已关闭,并发生严重错误

android - 重新创建一个创建的 fragment ?

java - IllegalArgumentException : invalid type for value: class android. widget.SeekBar

php - 未定义索引 : user when I display notification json array in laravel

objective-c - Objective C - 如何使用 NSUserNotification 标识符属性