在类之间移动而不创建新实例
有谁知道如何在不创建新实例的情况下显示另一个类? 从内存管理的角度来看,每次您想要显示不同的表单/页面时,您都需要使用 StartActivity ,然后它会创建该类的新实例,而不是重用以前创建的实例,这似乎有点疯狂。
提前致谢,
我猜从已经说过的内容来看,没有真正的方法可以做到这一点,不会妨碍操作系统的“返回”功能? 我正在构建一个线性应用程序,除了在每个屏幕上它都有一个主页按钮,然后可以取消此功能并以循环结束 - 无论如何,您是否知道销毁所有视图并重置回主界面班级? (IE 可以防止内存泄漏成为问题,但也不会损坏操作系统功能)
将其视为“清晰的历史记录”,而无需重新启动应用程序
Does anyone know of a way to show another class without creating a new instance?
It seems a bit crazy from a memory management point of view that each time you want to display a different form / page you need to use StartActivity which then creates a new instances of the class instead of reusing instances previously created.
Thanks in advance
I guess from what has been said - there is no real way to do it which won't hinder the "Back" functionality of the OS?
I'm building an app which is linear except on each screen it has a home button which then makes it possible to countermand this functionality and end in a loop - is there anyway you know of to destroy all over views and reset back to the main class? (IE prevent a memory leak from becoming a problem but also not damaging OS functionality)
Consider it a "clear history" without restarting the app
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否适用于 Android(来自 MS/C# 背景),但从概念上讲,一种选择是迭代打开的表单,寻找具有特定句柄的表单。然后,一旦找到它,只需调用该方法即可显示该表单。这取决于是否存在与 .NET 中的 Application.OpenForms 属性等效的 Java。
Not sure if this would work for Android (coming from a MS/C# background), but conceptually one option is to iterate through open forms looking for one with a specific handle. Then, once you find it, simply call the method to show that form. This would depend on there being a Java equivalent to the Application.OpenForms property in .NET.
从战术上讲,只要您从导航的角度了解其影响,就可以添加
FLAG_REORDER_TO_FRONT
将现有活动带回到前台。不过,你的问题很好奇。您如何访问 StackOverflow?
显然这不是通过网络浏览器进行的。 Web 浏览器使用您认为“疯狂”的确切机制,即使以前已查看过该网页,也会呈现该网页。他们这样做已经超过 15 年了,我们也一直做得很好。
Android 导航模型的设计大致反映了 Web 的导航模型:
通过“重用以前创建的实例”,您正在绕过该导航模型。例如,假设您的活动堆栈为 ABCD,并且您使用 B 和 FLAG_REORDER_TO_FRONT 的
Intent
调用startActivity()
。现在,活动堆栈是 ACDB。当用户按 BACK 多次时,他们不再在原来查看的 B 上,而是回到 A 上。在浏览器中,这将是相当奇怪的行为。清单中的
Intent
上还有其他标志或
上的属性,可提供“重用先前创建的实例”。然而,“从内存管理的角度来看”它们并不存在。它们是传统的以 Web BACK 为主的导航模式无法满足您的需求的地方。假设您没有搞砸任何地方,Android 将销毁未充分利用的活动,对内存进行垃圾收集,甚至将内存返回给操作系统。
Tactically, you are welcome to add
FLAG_REORDER_TO_FRONT
to bring an existing activity back to the foreground, so long as you understand the ramifications from a navigation standpoint.However, your question is rather curious. How are you accessing StackOverflow?
Clearly it's not via a Web browser. Web browsers use the exact mechanism that you feel is "crazy", rendering Web pages even if that Web page had been viewed previously. They have been doing so for over 15 years, and we've been doing OK by it.
The Android navigation model is designed to approximately mirror that of the Web:
By "reusing instances previously created", you're circumventing that navigational model. For example, let's suppose your activity stack were A-B-C-D, and you call
startActivity()
with anIntent
for B andFLAG_REORDER_TO_FRONT
. Now, the activity stack is A-C-D-B. When the user presses BACK times, they no longer are on the B they were looking at originally, but are back at A. In a browser, this would be rather strange behavior.There are other flags on
Intent
, or attributes on<activity>
in the manifest, that offer "reusing instances previously created". However, they are not there "from a memory management point of view". They are there where the traditional Web BACK-heavy navigation pattern does not fit your needs.Assuming you aren't screwing up anywhere, Android will destroy under-utilized activities, garbage collect that memory, and even return that memory to the OS.