我有一个问题令我烦恼。
在 Android 中,我有一个应用程序,每次创建活动的新实例时,它似乎都会调用 onCreate
,因为该活动是另一个活动的儿子,并且在结束其活动时会自行完成一个数据的角色。
每次调用 onCreate
似乎都是在重新解析 XML 以创建视图,并将侦听器重新附加/重新创建到代码等。 那么有没有一种方法可以缓存每次调用的 Activity,而在我们单击移动设备中的后退按钮时却看不到它呢? 这就像将其真正隐藏起来(无法通过后退按钮访问)并仅在必要时才将其销毁。
预先感谢大家:)
I've got a question that is annoying me.
With Android, I've got an application that seems to be calling onCreate
every time a new instance of an activity is created, because this activity is the son of an another and it finishes itself while it ends its role for one piece of data.
The calling of onCreate
every time seems to be re-parsing the XML to create the views and re-attaching/re-creating the listeners to the code and so on. So Is there a way to cache an activity to be recalled every time, without seeing it if we click on the back button in the mobile device? It's like having it really hidden (not accessible with the back button) and destroyed only if it's necessary.
Thanks everyone in advance :)
发布评论
评论(3)
Android 将处理最适用的视图和活动缓存,您不必担心它。 我强烈警告阿莱克斯基帕的做法。 这是非常幼稚的,只会降低性能并造成内存泄漏和错误行为。
如果您觉得 Activity 的创建过于频繁,我建议您确保正确使用和理解 Activity 生命周期。 与每个系统一样,Android 在效率和可重用性之间取得平衡,并且总是需要做出权衡。
您提到的一个常见误解涉及解析 XML。 视图的 XML 不存储为纯 XML,而是以编译形式存储以提高效率。 您不必担心这一点,相信系统会尽力为您提高效率。
Android will handle most applicable View and activity caching and you shouldn't have to worry about it. I -strongly- warn against alexkipar's approach. This is quite naive and will only worsen performance as well as create memory leaks and flat out wrong behavior.
If you feel Activity's are being created too often, I would recommend making sure you are using and understanding the Activity lifecycle correctly. As with every system, Android balances between efficiency and reusability, and there are always tradeoffs to make.
A common misconception you mentioned involves parsing XML. XML for views is not stored as pure XML, but rather in a compiled form for efficiency. You do not have to worry about this, trust that the system is trying to make things as efficient as it can for you.
如果它被调用,则意味着您的进程已完成执行或被系统终止。
onCreate
将在每次 Activity 启动时被调用。 这就是 Activity 生命周期 的工作方式,您无能为力。如果您希望您的进程保留在内存中,那么您应该考虑将其实现为 服务。
If it gets called it means your process finished executing or was killed by the system.
onCreate
will get called each time your activity starts. That's just the way the Activity lifecycle works and there's not much you can do there.If you wish that your process remain in memory then you should consider implementing it as a Service.
您无法阻止在每个 Activity 创建时调用 onCreate,因为这就是事情的工作方式。
这里你的问题更多的是一个架构问题:你有一个经常使用的功能,但你为它投入了整个繁重的活动。
如果您正在寻找速度和响应能力,答案可以是 ViewFlipper。 您定义一个独特的活动,当您使用您的功能时,您只需更改视图即可。 整个应用程序从一开始就被加载,然后它会保留在内存中,直到 Dalvik 需要一些为止。 您可以在开源应用 astrid 的代码中找到一个相当复杂的示例。
You can not prevent onCreate from being called at every Activity creation because that is the way things work.
Your problem here is more an architecture problem : you have a feature that you use very often but you dedicate a whole heavy activity to it.
If you are looking for speed and responsiveness, an answser can be the ViewFlipper. You define an unique Activity, and when you use your feature, you just change the view. You entire app is loaded at the very start, then it stays in memory until Dalvik needs some. You can find a pretty complex example in the code of the open source app astrid.