我如何“导航”?到已经在运行的活动?

发布于 2024-10-26 17:22:31 字数 363 浏览 7 评论 0原文

新手问题...所以我有两个活动,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

像极了他 2024-11-02 17:22:31

FLAG_ACTIVITY_REORDER_TO_FRONT 怎么样?

FLAG_ACTIVITY_CLEAR_TOP 也非常有用,在返回堆栈上完成活动,直到达到目标活性。

需要明确的是,即使使用上面的标志,您的活动也可能重新启动。如果您的活动在尝试释放内存时被破坏,就会发生这种情况。换句话说,您仍然需要确保您的 Activity 可以处理重新启动,并在 onPauseonSaveInstanceState 中采取适当的预防措施。

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 and onSaveInstanceState.

jJeQQOZ5 2024-11-02 17:22:31

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文