Welcome Android Developpers

Welcome Android Developpers
My name is Mathias Séguy, I am an Android Expert and Trainer and created the Android2EE company.

For full information, it’s here : Android2EE's Training
You can meet me on Google+, follow me on Twitter or on LinkedIn

Saturday, December 24, 2011

Merry Christmas




Merry christmas to you.
May Santa's spirit be with you :o)

Saturday, December 17, 2011

Handler and activity's lifecycle available tutorials

Bandeau Conference
Hello,
You can find on Android2ee 3 tutorials that show you:

  • How to use Handler and manage its life cycle using AtomicBooleans
  • How to use Handler and manage its life cycle using onRetainNonConfigurationInstance method
  • A demonstration of the memory leak associated to the non management of the Handler life cycle.
Those Eclipse projects are downlaodable here : Hanlder Tutorials


So, Thanks who?
Thanks, Android2ee, the Android Programming Ebooks :o)

Mathias Séguy
mathias.seguy.it@gmail.com
Auteur Android2EE
Ebooks to learn Android Programming.
Android Trainings for Enterprise
AndroidEvolution
Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo

Thursday, December 15, 2011

Handler And LifeCycle Part III

Bandeau Conference

Hello again,
To continue with the issue of Handler and activity's life cycle, there is a solution, advocated by some, that is to use the public method Object onRetainNonConfigurationInstance () to return a pointer to an object in the activity.
Uh, let me explain, when your activity is to be destroyed and immediately recreates the method onRetainNonConfigurationInstance can send an object from the instance of the dying activity to the instance of new activity.
Example adapted to our problem:


@Override
public Object onRetainNonConfigurationInstance() {
                //Save the thread
                return backgroundThread;
}

to retrieve the object in the onCreate method:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
//Do something
                backgroundThread = (Thread) getLastNonConfigurationInstance();
//Do something again
}


It is clear that with this method everything seems solved (at least for the change in orientation of the device). And yes, BUT NOT!!!, it's a serious mistake to think that. Indeed, what happens if your activity is killed (without being recreated immediately)? Well, then your thread is an orphan, your handler and your activity become ghosts (because the garbage collector detects them as used). And here, again, Darth Vader mocks.

The solution, and yes, there is one, is somewhat more complex than that. We must re-implement an atomic boolean to kill the thread if your activity is not restarted immediately.

And then, well, I invite you to get the tutorials that I dropped on Android2EE section Example/Tutorials/Handler's Tutorials. I give you 3 tutorials, one with the Atomic booleans, one with the onRetainNonConfigurationInstance instance and the last is a demonstration of the memory leaks.

But if your want the code, here it is:

Handlers, Threads and Activity's life cycle

Bandeau Conference
Hello,
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)

Mathias Séguy
mathias.seguy.it@gmail.com
Auteur Android2EE
Ebooks to learn Android Programming.
AndroidEvolution
Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo