Unfortunately, using onRetainNonConfigurationInstance/getLastNonConfigurationInstance can involve leaks.
In my case (I use an IntentService to upload a file on a remote server), I was declaring my connection in the considered Activity such as :
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mServiceMessenger = new Messenger(service);
mConnectedToService = true;
}
public void onServiceDisconnected(ComponentName className) {
mConnectedToService = false;
mServiceMessenger = null;
}
};
The mServiceMessenger is an instance of Messenger that allows me to send a cancel order to the upload task.
Nevertheless, when I tested the solution using onRetainNonConfigurationInstance and getLastNonConfigurationInstance, I tracked (thanks to MAT plug-in in Eclipse) that a screen rotation involves a lot of leaks of my Activity context.
To solve this problem (and because my Application complexity allows me to do so), I created a singleton grouping together all the elements I need to handle the connection to my IntentService (and communication with my Activity). So, when rotating the screen, the new Activity created gets back the connection managed by the singleton and can use it without losing information.