[英]Android Notification click keep stack and bring app to front if need. Pass data if specific activity is on top
How can I click a notification, bring my app to the foreground if needed, and keep the stack as it was (even when the app went to the background)?如何单击通知,在需要时将我的应用程序置于前台,并保持堆栈不变(即使应用程序进入后台)?
I have a NotificationClickActivity
that is launched when the user clicks a notification.我有一个NotificationClickActivity
在用户单击通知时启动。
When the notification is clicked, I have two possible scenarios:单击通知时,我有两种可能的情况:
In the first scenario, NotificationClickActivity
starts the login process and receives the result.在第一个场景中, NotificationClickActivity
启动登录过程并接收结果。 If OK, I launch MainActivity
.如果可以,我启动MainActivity
。 The task is cleared so MainActivity
is the only activity that I have in the task.任务被清除,所以MainActivity
是我在任务中的唯一活动。 I have no problems in this scenario.在这种情况下我没有问题。
In the second scenario NotificationClickActivity
does this:在第二种情况下NotificationClickActivity
这样做:
finish()
startActivity(MainActivity.createIntent(this).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra("notification", notification))
With the above code, if the app is already running ( MainActivity
is the root and top activity) and if on the background or in the foreground, I have no problems.使用上面的代码,如果应用程序已经在运行( MainActivity
是根和顶级活动)并且如果在后台或前台,我没有问题。 MainActivity
onNewIntent
is called and I can do whatever I what with the notification. MainActivity
onNewIntent
被调用,我可以对通知做任何事情。 In this case, I dispatch the notification to a repository.在这种情况下,我将通知发送到存储库。
The problem I'm facing is when I have activities on top of MainActivity
.我面临的问题是当我在MainActivity
之上进行活动时。 Imagine the following stack:想象一下下面的堆栈:
MainActivity -> ActivityOne -> ActivityTwo -> ActivityN
With the code that I currently have if I click the notification the stack becomes:使用我当前拥有的代码,如果我单击通知,堆栈将变为:
MainActivity -> ActivityOne -> ActivityTwo -> ActivityN -> MainActivity
I want the stack to stay MainActivity -> ActivityOne -> ActivityTwo -> ActivityN
but still pass the notification to MainActivity
so it can be dispatched to the repository.我希望堆栈保持MainActivity -> ActivityOne -> ActivityTwo -> ActivityN
但仍将通知传递给MainActivity
以便可以将其分派到存储库。
Any idea how can I achieve this?知道如何实现这一目标吗?
I've been playing with the intent flags for some time but without success.我已经玩了一段时间的意图标志,但没有成功。 Of course, I what this without any visible animations or whatever so that the user does not realize what is going on.当然,我没有任何可见的动画或其他任何东西,这样用户就不会意识到发生了什么。
Think you're looking for FLAG_ACTIVITY_REORDER_TO_FRONT
认为您正在寻找FLAG_ACTIVITY_REORDER_TO_FRONT
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP || FLAG_ACTIVITY_REORDER_TO_FRONT)
This will bring the activity to the top of the stack if its already running.如果它已经在运行,这会将活动带到堆栈的顶部。
consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B.考虑一个由四个活动组成的任务:A、B、C、D。如果 D 调用 startActivity() 的 Intent 解析为活动 B 的组件,则 B 将被带到历史堆栈的前面,结果如下订购:A、C、D、B。
In the above example, if you want to go from A,B,C,D to A,B,C,D,B instead, then you need to make sure your activity supports multiple instances.在上面的例子中,如果你想 go 从 A,B,C,D 到 A,B,C,D,B 代替,那么你需要确保你的活动支持多个实例。
Suggest FLAG_ACTIVITY_SINGLE_TOP
to be specified in android manifest for the activity instead.建议改为在FLAG_ACTIVITY_SINGLE_TOP
清单中为活动指定 FLAG_ACTIVITY_SINGLE_TOP。 Then in your onNewIntent
you should check if the user is logged in. If the user is not, you should launch the login process with startActivityForResult
and handle the login result with onActivityResult
.然后在您的onNewIntent
中,您应该检查用户是否已登录。如果用户未登录,您应该使用startActivityForResult
启动登录过程并使用onActivityResult
处理登录结果。
This assumes your login can be initiated with an intent, which calls setResult
with enough information for the caller to determine if the login was successful.这假设您的登录可以通过一个意图启动,该意图调用setResult
并提供足够的信息供调用者确定登录是否成功。
As for animations, I think you will have to use fragments and disable animations, or disable animations for the activity, and do custom transitions only when you want it.至于动画,我认为您将不得不使用片段并禁用动画,或者禁用活动的动画,并且仅在需要时进行自定义转换。 This may even not work with activities, since the OS/OEM defined transitions may just happen with activities regardless anyway, so you'll very likely have to make sure rest of the app is in the same activity and use fragments for navigation.这甚至可能不适用于活动,因为无论如何,操作系统/OEM 定义的转换可能只会发生在活动中,因此您很可能必须确保应用程序的 rest 处于同一活动中并使用片段进行导航。
This is not possible with this approach.使用这种方法是不可能的。 If the current task contains MainActivity
and also other activities on top of that, there is no way to send an Intent
to MainActivity
(without removing the activities on top of MainActivty
) using startActivity
or by launching an Activity
.如果当前任务包含MainActivity
以及除此之外的其他活动,则无法使用startActivity
或启动Activity
将Intent
发送到MainActivity
(不删除MainActivty
之上的活动)。
If you need to get information to MainActivity
then you need to use another mechanism to do this:如果您需要获取MainActivity
的信息,那么您需要使用另一种机制来执行此操作:
Intent
and have MainActivity
listen for the broadcast发送广播Intent
并让MainActivity
监听广播
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.