android-通知单击-如果应用程序关闭了打开活动,如果应用程序打开带来了显示当前活动 [英] android - notification click - if app closed open activity, if app opened bring show current activity

查看:114
本文介绍了android-通知单击-如果应用程序关闭了打开活动,如果应用程序打开带来了显示当前活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项正在进行的通知服务.在应用程序本身中,我有两个活动-HomeActivitySettingsActivity.

I have a service with an ongoing notification. in the app itself I have two activities - HomeActivity and SettingsActivity.

当前我所拥有的是->单击通知时:

Currently what I have is -> when the notification is clicked:

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

如果当前的显示活动是HomeActivity,请将其放在最前面而不创建新的活动.

If the current showing activity is HomeActivity, bring it to front without creating a new one.

代码:

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();

清单::我添加了"android:launchMode ="singleTop":

Manifest: I added `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实例.

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

我想要的是在任一活动中打开应用并单击通知时->显示当前活动,并且如果 app已关闭并单击通知,则打开HomeActivity的新实例.

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.

推荐答案

当我进入SettingsActivity并单击通知时,它将打开HomeActivity的新实例.

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

因为这是singleTop的用途:

但是,如果目标任务已经具有该任务的现有实例, 活动位于其堆栈的顶部,该实例将收到新的 目的(在onNewIntent()调用中);不会创建一个新实例.在 其他情况-例如,如果 "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.

which is your case because what you have on top is SettingsActivity.

我想要的是在活动中打开应用并单击通知时显示->显示当前活动,并且如果应用已关闭并单击通知,则打开HomeActivity的新实例

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

然后,您的通知也许应该将意图发送给第3个活动(无ui),然后将其进一步重定向到finish().根据您的targetSdk是什么,您可能需要使用 ActivityLifecycleCallbacks 来找出您的应用排在最前的位置.

Then your notification should perhaps send intent to 3rd activity (w/o ui) which would then redirect further and finish(). Depending on what is your targetSdk you may want to use ActivityLifecycleCallbacks to figure out when your app is front most one or not.

这篇关于android-通知单击-如果应用程序关闭了打开活动,如果应用程序打开带来了显示当前活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆