[英]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
实例放在最前面。
TextChatActivity
) A and B, and B is on front. 假设我有2个公开对话( TextChatActivity
)A和B,而B在前面。 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.