Reputation: 957
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?
Upvotes: 0
Views: 943
Reputation: 10298
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.
Upvotes: 1