单击通知时重用当前应用程序实例 [英] Reuse current app instance when clicking on Notification

查看:66
本文介绍了单击通知时重用当前应用程序实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在使用PendingIntentNotification启动所需的Activity.但是我想知道是否有可能重用当前的应用程序实例.

Right now I'm using a PendingIntent to launch my desired Activity from the Notification. But I want to know if it's possible to reuse the current app instance.

例如:

  • 启动应用程序,启动器活动称为HomeActivity
  • 导航到SecondActivity
  • 按下主页按钮
  • 点击应用中的通知,然后恢复SecondActivity
  • Launch app, the Launcher Activity is called HomeActivity
  • Navigate to SecondActivity
  • Press the home button
  • Click on the notification from the app and resume SecondActivity

但是,如果用户没有导航到SecondActivity,我想在单击通知时打开HomeActivity.我很乐意为您提供有关此问题的帮助:)

But if the user didn't navigate to the SecondActivity i want to open the HomeActivity when I click on the Notification. I would be glad for any help regarding this problem :)

推荐答案

但是我想知道是否可以重用当前的应用实例.

But I want to know if it's possible to reuse the current app instance.

是的,如果应用程序在后台运行,则可以重复使用.在 PendingIntent 中,您将传递一个 Intent ,因此,在该 Intent 中,您应设置一个类似intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);的标志,以便该应用程序在后台运行时只会把活动摆在最前面.

Yes, you can reuse if the app is in background. In PendingIntent you will pass an Intent, so in that Intent you should set a flag like intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); so if the app is in background it will just bring activity to front.

根据android文档:

According to android docs :

FLAG_ACTIVITY_NEW_TASK

在API级别1中添加int FLAG_ACTIVITY_NEW_TASK(如果已设置),此活动 将成为此历史记录堆栈上新任务的开始.一个任务 (从启动它的活动到下一个任务活动)定义 用户可以移动到的一系列原子活动.任务可以是 移至前景和背景;内部的所有活动 特定任务的名称始终保持相同的顺序.请参阅任务和 返回堆栈以获取有关任务的更多信息.

Added in API level 1 int FLAG_ACTIVITY_NEW_TASK If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order. See Tasks and Back Stack for more information about tasks.

该标志通常用于想要展示 启动器"风格的行为:它们为用户提供了一个单独的列表 可以完成的事情,否则将完全独立运行 启动它们的活动.

This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.

使用此标志时,如果活动的任务已经在运行 您现在开始,则新的活动将不会开始; 而是将当前任务简单地带到 屏幕及其上次进入的状态.请参见FLAG_ACTIVITY_MULTIPLE_TASK 标记来禁用此行为.

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

当呼叫者从以下位置请求结果时,不能使用此标志 正在启动的活动.

This flag can not be used when the caller is requesting a result from the activity being launched.

此处

但是,如果用户未导航到 SecondActivity ,我想打开 HomeActivity .

如果您创建的通知意图类具有 SecondActivity 作为启动器,为什么您要启动 HomeActivity .它将创建一个新的 SecondActivity ,如果它不在后台,则将其启动.

Why do you launch HomeActivity if your notification intent class you are creating have SecondActivity as the launcher. It will create a new SecondActivity and launches it if it is not in background.


如果您想重新启动应用程序,请尝试以下操作:


If you want to relaunch app where you left off try like this :

final Intent resumeIntent = new Intent(context, YourLauncher.class);
resumeIntent.setAction("android.intent.action.MAIN"); 
resumeIntent.addCategory("android.intent.category.LAUNCHER"‌​);
final PendingIntent resumePendingIntent = PendingIntent.getActivity(context, 0, resumeIntent, 0);

这里YourLauncher.class是启动器类的名称.

Here YourLauncher.class is the launcher class name.

这篇关于单击通知时重用当前应用程序实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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