如何移除Activity Stack底部的Activity?

发布于 2024-10-18 14:13:20 字数 250 浏览 4 评论 0原文

在我的应用程序中,我有大约十几个活动。然而,其中大多数都有一些快捷按钮,因此用户可以从应用程序中的任何位置启动最重要的按钮。这意味着,用户可以在应用程序中跳转,用“活动”填充“活动堆栈”。我无法改变这种行为,因为我需要堆栈提供的历史功能(所以我不能只使用 noHistory 选项)。

显然,我想针对这种可能的内存泄漏采取一些措施。如果堆栈的大小大于某个数字,是否可以从活动堆栈底部删除该条目?那么堆栈只记住最新的X(例如30)个Activity?

谢谢

In my application I have about a dozen of Activities. However, most of these have some shortcut-buttons, so the user can start the most important ones from anywhere in the app. This means, that the user can just jump around the app., filling up the Activitiy Stack with Activities. I can't change this behaviour, because I need the history function the stack provides (so I can't just use the noHistory option).

Obviously, I'd like to do something against this possible memory leak. Is it possible to remove the entry from the bottom of the Activity Stack, if the stack's size is greater than a certain number? So the stack only remembers the newest X (e.g. 30) Activity?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

青萝楚歌 2024-10-25 14:13:20

显然,我想针对这种可能的内存泄漏采取一些措施。

有什么可能的内存泄漏? Android 会根据需要销毁 Activity 以回收内存。如果用户按后退按钮到达一个被销毁的 Android,则该 Activity 将使用其原始 Intent 重新创建,并传递您通过 onSaveInstanceState() 填写的 Bundle。

如果堆栈的大小大于某个数字,是否可以从活动堆栈底部删除该条目?那么堆栈只记住最新的X(例如30)个Activity?

据我所知,情况并非如此。再说一次,你不应该需要它。

Obviously, I'd like to do something against this possible memory leak.

What possible memory leak? Android will destroy activities as needed to reclaim memory. If the user BACK-buttons their way to one Android destroyed, the activity will be recreated with its original Intent and passed the Bundle you filled in via onSaveInstanceState().

Is it possible to remove the entry from the bottom of the Activity Stack, if the stack's size is greater than a certain number? So the stack only remembers the newest X (e.g. 30) Activity?

Not that I am aware of. Again, you should not need it.

枯叶蝶 2024-10-25 14:13:20

History Stack将由Android Runtime管理;你不必担心这一点。

但是,如果您之前启动了某个 Activity 并希望将其置于最前面,则可以通过设置适当的 意图标志(如 ACTIVITY_FLAG_REORDER_TO_FRONT)。这样,当不需要时,堆栈上就不会出现重复的 Activity。请注意,这将影响历史的年代/线性性质。

History Stack will be managed by Android Runtime; you do not have to worry about that.

However, if you have launched an activity earlier and want to bring it to the front, you can do this by setting appropriate Intent Flags(like ACTIVITY_FLAG_REORDER_TO_FRONT). That way you would not have duplicate Activities on the stack when not desired. Note that, this will affect the chronology/linear nature of history.

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