0

Store application global data

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?

Community
  • 1
  • 1
user890973
  • 947
  • 2
  • 8
  • 12

1 Answers1

1

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.

Maor Hadad
  • 1,850
  • 21
  • 36
aleung
  • 9,848
  • 3
  • 55
  • 69
  • Another question,what's difference between foreground task and background task?When a foreground task become background task,I find that background task's main thread become wait.What else will android framework do? – user890973 Nov 30 '11 at 05:33
  • :) Thank you,I have read that article.AsyncTask sometimes make me pain,In such case as http://stackoverflow.com/questions/7034823/when-would-activitys-instance-die .Handler is a good way for manage a group of task,it makes manage sequence or prevent duplicate easily.Thanks for your help – user890973 Dec 01 '11 at 05:19