简体   繁体   English

管理Android活动堆栈:将特定的活动实例放在最前面

[英]Managing Android Activity stack: bring specific activity instance to front

I have a text message app which has two main activities: 我有一个短信应用程序,它有两个主要活动:

  • TextChatActivity - a conversation. TextChatActivity对话。 Can have multiple instances for multiple conversations. 可以有多个实例进行多个对话。
  • ConvListActivity - a list of all the activities. ConvListActivity所有活动的列表。

In addition, a TextChatActivity of a certain conversation can be opened via a status bar notification. 另外,可以通过状态栏通知打开某个对话的TextChatActivity

What I want to do is to bring a specific TextChatActivity instance to the front. 我要做的是将特定的TextChatActivity实例放在最前面。

  • Let's say I have 2 open conversations ( TextChatActivity ) A and B, and B is on front. 假设我有2个公开对话( TextChatActivity )A和B,而B在前面。
  • Now I got a notification that leads to conversation A. I want it to bring conversation A TextChatActivity to front. 现在,我收到一个通知,该通知导致对话A。我希望它将对话A TextChatActivity带到前面。

How can I do that without opening a new instance of TextChatActivity for conversation A? 如何在不为会话A打开TextChatActivity的新实例的情况下TextChatActivity这一点?

Thanks! 谢谢!

Actually, you can't. 实际上,您不能。 If you have multiple instances of an activity in the stack, there is no way to address a unique instance of an activity so that you could bring it to the front. 如果堆栈中有一个活动的多个实例,则无法解决一个活动的唯一实例,因此您可以将其置于最前面。

Your architecture is not good. 您的架构不好。 Because of the way Android works, you would be better off if you had a single instance of this activity, and allow the user to switch between conversations, not by creating a new instance of an activity, but just by switching out the underlying data for the existing activity. 由于Android的工作方式,如果您只有一个此活动实例,则更好,并且允许用户在对话之间进行切换,而不是通过创建活动的新实例,而只是将基础数据切换出现有活动。 In this way, you only ever have one instance of the activity and you can simply change the data that you are displaying. 这样,您将只有一个活动实例,并且您可以简单地更改要显示的数据。

In your "notification" example, the Intent that you start from the notification should have an "extra" that indicates which conversation the user wants to show. 在您的“通知”示例中,您从通知开始的Intent应该有一个“额外”,指示用户想要显示哪个对话。 You should ensure that there is only one instance of your TextChatActivity by declaring it with launchMode="singleTop" or by setting FLAG_ACTIVITY_SINGLE_TOP when you start it. 你应该确保有你的只有一个实例TextChatActivity用它声明launchMode="singleTop" ,或通过设置FLAG_ACTIVITY_SINGLE_TOP当你开始吧。 When onNewIntent() is called in your activity, check the "extras" and adjust the content to show the desired conversation. 在您的活动中调用onNewIntent() ,请检查“附加”并调整内容以显示所需的对话。

If you want to create the "illusion" of an activity per conversation, then you can override `onBackPressed() and manage your own "stack" of conversations in the activity, so that when a user presses the BACK key, you can go back in the "stack" of conversations and show him the previous one, just by modifying the data that is displayed. 如果要为每个对话创建活动的“幻觉”,则可以覆盖`onBackPressed()并管理活动中自己的对话“堆栈”,以便当用户按下BACK键时,您可以返回在对话的“堆栈”中,只需修改显示的数据即可向他显示上一个对话。

只需开始一个活动并设置标志FLAG_ACTIVITY_CLEAR_TOP

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

相关问题 管理android活动堆栈:活动的特定实例如何将自己摆在最前面? - Managing android activity stacks: How can a specific instance of an activity bring itself to the front? Android将活动带到了前面 - Android bring activity to front Android 通知单击保持堆栈并在需要时将应用程序置于最前面。 如果特定活动在顶部,则传递数据 - Android Notification click keep stack and bring app to front if need. Pass data if specific activity is on top 如何在Android中引入活动? - How to bring an activity to front in android? Android抢先关注当前活动 - Android bring to front current activity 将 Activity 从堆栈中移到最前面,但也要重新创建它 - Bring an Activity to front from the stack but also recreate it 在Android内,如何开始一项活动,或者如果该活动已经在堆栈中,那么该活动如何发挥作用? - Within Android, how do I start an activity or bring to front that activity if already on stack? Android-将应用程序类的活动放在最前面 - Android - Bring an activity to the front from application class Android-如何在暂停时重新启动活动(或将其置于最前面) - Android - How to restart activity ( or bring it to front ) on pause 在Android广播接收器中将活动置于最前面 - Bring an activity to front in android Broadcast Reciever
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM