Thursday, December 15, 2011
Handlers, Threads and Activity's life cycle
As I said earlier, when managing Handlers or AsynchTasks, you must always make sure to link the life cycle of the thread with the Handler's one which is also linked with the activity's one ... Otherwise, your thread becomes an orphan Thread.
Ok, digging a little bit more (I will drop you on Androi2EE an eclipse project that shows what I'm saying) it's worse, in fact: When passing by OnDestroy and onCreate (short I return the device), not only the thread becomes orphan, but also the Handler (arg, what it is not destroyed o_O ') and worse is that your activity becomes a ghost ... glooops.
And yes it's crazy, the first activity is not destroyed, she is called by the first thread through the first Handler (hence the absence of the raise of a NullPointer exception) and the second activity is created with the new Handler and its new Thread.
I mean, DarkVador, Voldemor and Sauron laughed thinking of the number of threads, Handlers, AsynchTask, and activities ghosts that inhabit the Android devices because developers unaware of the danger.
So: Manage your threads by linking their life cycle with that of the activity.
So, Thanks who?
Thanks, Android2ee, the Android Programming Ebooks :o)
Ebooks to learn Android Programming.
Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo