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
Showing posts with label orphan thread. Show all posts
Showing posts with label orphan thread. Show all posts

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:

Monday, November 21, 2011

Handler and Activity's life cycle, take care about orphan threads!!


Bandeau Conference
Hello,
You have an Activity which uses a Handler. You create your handler and overwrite the handleMessage method, you launch the handler’s thread and that’s it for the handler management… Most of us do such a thing and it’s a huge mistake!!! What happens to your thread when your activity pauses and resumes and worst when it dies and (re)creates? 
You thread becomes an orphan thread !

So you have written something like that :