我如何“导航”?到已经在运行的活动?
新手问题...所以我有两个活动,Cherry 和 Apple,每个活动上都有一个按钮可以转到另一个活动。如此来回。
在“Cherry”类中,我这样说:
intent = new Intent(Cherry.this, Apple.class)
startActivity(intent);
意思是它应该交给苹果。 Apple Activity 中也有类似的代码。
我想我看到的是,例如,每次我启动AppleActivity时,它都会启动它的一个新实例,而不仅仅是重新激活Apple。我已经搜索了文档,但找不到可以执行我想要的操作的标志或其他调用。
任何提示将不胜感激!
——皮托
Newbie question... So I have two activities, Cherry and Apple, and each one has a button on it to go to the other one. So back and forth.
In class "Cherry" I say this:
intent = new Intent(Cherry.this, Apple.class)
startActivity(intent);
Meaning that it should go to the Apple. There's similar code in the Apple activity.
What I think I am seeing is, is that each time when I startActivity Apple for example, it's starting a new instance of it instead of just reactivating Apple. I've scoured the doc and can't find the flag or other call that would do what I want.
Any tips would be appreciated!
-- Pito
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FLAG_ACTIVITY_REORDER_TO_FRONT 怎么样?
FLAG_ACTIVITY_CLEAR_TOP 也非常有用,在返回堆栈上完成活动,直到达到目标活性。
需要明确的是,即使使用上面的标志,您的活动也可能重新启动。如果您的活动在尝试释放内存时被破坏,就会发生这种情况。换句话说,您仍然需要确保您的 Activity 可以处理重新启动,并在
onPause
和onSaveInstanceState
中采取适当的预防措施。What about FLAG_ACTIVITY_REORDER_TO_FRONT?
FLAG_ACTIVITY_CLEAR_TOP is also quite useful, finishing activities on the back stack until the target activity is reached.
To be clear, your activity may be restarted even if using the flags above. This would happen if your activity were destroyed in an attempt to free memory. In other words, you still need to make sure your activity can handle being restarted, taking the proper precautions in
onPause
andonSaveInstanceState
.如果 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 已经按照这里给定的方式运行,则它将把启动的 Activity 返回到前面< /a>.您必须自己处理后退按钮,以免完成当前活动。
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT will bring the launched activity back to front if it's already running as given here. You will have to handle the back button yourself so as not to finish the current activity.