简体   繁体   English

Android 通知单击保持堆栈并在需要时将应用程序置于最前面。 如果特定活动在顶部,则传递数据

[英]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:单击通知时,我有两种可能的情况:

  1. User is logged out from the app用户已从应用程序中注销
  2. User is logged in.用户已登录。

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或启动ActivityIntent发送到MainActivity (不删除MainActivty之上的活动)。

If you need to get information to MainActivity then you need to use another mechanism to do this:如果您需要获取MainActivity的信息,那么您需要使用另一种机制来执行此操作:

  • Send a broadcast Intent and have MainActivity listen for the broadcast发送广播Intent并让MainActivity监听广播
  • Use an event bus implementation使用事件总线实现
  • Use a "global variable" to pass this information between the 2 activities使用“全局变量”在 2 个活动之间传递此信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 管理Android活动堆栈:将特定的活动实例放在最前面 - Managing Android Activity stack: bring specific activity instance to front 通知将应用程序带到前面,无需更改活动 - Notification bring app to front without change activity android:通过通知将当前活动置于最前面 - android: bring current activity to front via notification Android:使用 Viewpager 将 Activity 置于最前面的通知 - Android: Notification to bring Activity to front with Viewpager Android通知 - 如何在不调用活动的情况下将活动置于最前面 - Android Notification - How to bring the activity to front without recall the activity android-仅在堆栈中存在活动的情况下,才将活动置于堆栈顶部 - android- bring a activity to top of stack ONLY IF activity exists on stack Android将活动带到了前面 - Android bring activity to front 如何通过通知将Android现有活动放在首位 - How to bring Android existing activity to front via notification Android OneSignal-单击通知后将应用程序置于最前面 - Android OneSignal - Bring app to front after clicking notification Firebase (FCM):打开活动并在通知点击时传递数据。 android - Firebase (FCM): open activity and pass data on notification click. android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM