当应用程序进入后台时,它的静态实例可能会被清除吗?
有人说当任务进入后台时,android会清除单例实例。 这是真的吗?
我意识到,当我尝试终止前台任务(通过使用 DDMS)时,应用程序会自动重新启动它。一定有一些守护进程,不是吗?
当然,重新启动前台应用程序对我来说是安全的,因为当进程重新启动时,我可以通过调用应用程序的 onCreate 来重新初始化我的应用程序。
但我对后台任务/应用程序感到困惑。android会像前台应用程序一样杀死后台任务并重新启动它吗?(我尝试杀死后台应用程序,它退出而不重新启动)。或者dalvik会清除并回收静态实例?
如果dalvik真的清除了singlton,我该如何避免它?
Someone said that android will clear singleton instance when task bring to background.
Is it true?
I realize that when I try to kill a foreground task(by using DDMS),the application auto restart it.There must be some deamon,isn't it?
Ofcouse,restart foreground application is safe to me,because as process restart,I can reInitialize my app by call Application's onCreate.
But I'm confused about background task/application.Will android kill background task and restart it just the same way as foreground app?(I hava try to kill background application,it exit without restart).Or will dalvik clear and recycle static instance?
If dalvik really really clear singlton,how do I avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个Android应用程序都在一个进程中运行。当一个任务(无论后台还是前台)被杀死时,实际上进程也被杀死了。就像Java应用程序运行在JVM上一样,每个JVM实例都是一个进程。
与 JVM 不同,dalvik 对象管理并没有什么魔力。我不认为 dalvik 会清除单例实例。没有引用的对象实例在 GC 上会很清晰,但单例则不然。
在 Android 应用程序中,主线程是事件调度线程。它循环运行,将事件分派到适当的活动、小部件或服务。编写应用程序实际上是实现事件回调:您编写的代码中没有 main(),您永远不会拥有主线程,底层框架在事件发生时调用您的代码。当任务转到后台时,即没有任何可见活动时,不会生成 UI 事件,因此您会看到主线程正在事件队列上等待。 Painless Threading 文章讨论了 Android 应用程序使用的线程模型。
Each Android application is running in a process. When a task (no matter background or foreground) is killed, actually the process is killed. It's just like the case that Java application runs on JVM, each JVM instance is a process.
There is no magic in dalvik object management which is different to JVM. I don't think dalvik will clear singleton instance. Object instance without reference will be clear on GC, but singleton should not.
In an Android application the main thread is event dispatch thread. It runs in loop, dispatches events to appropriate activities, widgets or services. Writing an application is actually implementing event callbacks: there is no main() in the code you write, you never own the main thread, the underlay framework calls your code when event happens. When the task turns to background, that is without any activities visible, there is no UI event generated, so you see that the main thread is waiting on the event queue. The article Painless Threading discusses the threading model used by Android applications.