管理Android活动堆栈:将特定的活动实例放在最前面 [英] Managing Android Activity stack: bring specific activity instance to front

查看:95
本文介绍了管理Android活动堆栈:将特定的活动实例放在最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个短信应用程序,它有两个主要活动:

I have a text message app which has two main activities:

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

此外,可以通过状态栏通知打开特定会话的TextChatActivity.

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.

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

如何为会话A打开新的TextChatActivity实例而不怎么做?

How can I do that without opening a new instance of TextChatActivity for conversation A?

谢谢!

推荐答案

实际上,您不能.如果堆栈中有一个活动的多个实例,则无法解决一个活动的唯一实例,因此您可以将其置于最前面.

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.

您的架构不好.由于Android的工作方式,如果您只有一个此活动实例,并且允许用户在对话之间进行切换,而不是通过创建活动的新实例,而只是将基础数据切换出该状态,则效果会更好.现有活动.这样,您将只有一个活动实例,并且只需更改显示的数据即可.

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. In this way, you only ever have one instance of the activity and you can simply change the data that you are displaying.

在通知"示例中,您从通知开始的Intent应该有一个"extra",指示用户想要显示哪个对话.通过使用launchMode="singleTop"进行声明或在启动时设置FLAG_ACTIVITY_SINGLE_TOP,应确保只有一个TextChatActivity实例.在您的活动中调用onNewIntent()时,请选中附加"并调整内容以显示所需的对话.

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. 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. When onNewIntent() is called in your activity, check the "extras" and adjust the content to show the desired conversation.

如果您想为每个对话创建活动的幻觉",则可以覆盖`onBackPressed()并管理活动中自己的对话堆栈",这样,当用户按下BACK键时,您可以只需修改显示的数据,就可以回到对话的堆栈"并向他显示上一个对话.

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.

这篇关于管理Android活动堆栈:将特定的活动实例放在最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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