java - 打开通知时如何更新 Activity

标签 java android

我正在处理通知,但遇到了问题。当我点击通知时我有一个 Activity 已经打开我不想再次打开它只是更新当前 Activity 。

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
 Intent pushNotification = null;
                if (NotificationType != 0 && NotificationType != 2 && NotificationType != 5 && NotificationType != 26) {
                    pushNotification = new Intent(getApplication(), SplashScreen.class);
                    pushNotification.putExtra("NotificationType", NotificationType);
                    pushNotification.putExtra("ReferenceID", ReferenceID);
                    pushNotification.putExtra("NotificationID", ReferenceID);
                    pushNotification.putExtra("isread", ReferenceID);
                    showNotificationMessage(getApplicationContext(), title, message, time, pushNotification);
                } else if (NotificationType == 0 || NotificationType == 2 || NotificationType == 5 || NotificationType == 26) {
                    showNotificationMessageWithNoAction(getApplicationContext(), title, message, title, null);
                }
}

谁能告诉我点击通知时如何更新 Activity ?

最佳答案


你只需要声明launchModesingleTask来确保多个相同的屏幕不会打开。

Activity 有四种启动模式。他们是:
1.标准
2.单顶
3.单任务
4. 单实例

请引用此链接 Click here

<activity android:name="YOUR_SPLASH_ACTIVITY"
            android:launchMode="singleTask"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

而在 Java 代码中,您只需覆盖 onNewIntent 方法,以刷新 activity

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    /**
     * HERE YOU JUST REFRESH , YOUR ACTIVITY 
     */

}

关于java - 打开通知时如何更新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55378325/

相关文章:

java - 外部硬件信息

java - 使用 Java 配置在 Spring 中处理多个配置文件的最佳实践是什么?

java - 如何使用 JavaSound (Java, Java Sound) 从 .wave 文件读取信息

javascript - 安装 ionic 应用程序后看不到 ionic 应用程序图标

java - Android Activity 中未调用 main 方法

android - 动态选项卡插件

android - java.lang.IllegalStateException : The specified child already has a parent. 您必须先在 child 的 parent 上调用 removeView()

Android - 如何在 SQLite 数据库中存储 html 字符串

java - 使用Picasso通过DataObject在RecyclerView中加载图像

java - Spring Kafka 生产者抛出 TimeoutExceptions