I am still trying to find the "correct" design pattern when dealing with AsyncTask
s and screen rotation. I read this commonsware blog post which links to this code, but the onRetainNonConfigurationInstance method is now deprecated. The magic sentence in the documentation states "The guarantee of no message handling during the switch to the next activity simplifies use with active objects..." which is a truly critical part about what this method did. I don't see its suggestion of using setRetainInstance() being able to accomplish the same goal.
I also saw posts such as this one whose number one answer is really nothing short of a bad hack which only covers 90% of use cases (ie. what happens if your task is running and then a phone call comes in, that solution will not work).
It was suggested to review the code here for AsyncTask examples but unless I am being dense, I don't see them being used anywhere in the application (when using the search feature on google code)
This question has obviously been asked many times but I have not seen an up to date, proper asnwer. If one exists, please answer and close as duplicate if you must, but at least answer! :)
This is another link which reviews the same, deprecated method.
It's only deprecated on Honeycomb and above, and it will work just fine on those too. The 'new' way is to use loaders (you can use the compatibility library to get those in pre-HC versions) or retained fragments. If you call setRetainInstance()
the instance passed as is to the newly created activity (they actually use onRetainNonConfigurationInstance
in the FragmentActivity
of the compatibility library), so it's effectively the same as what you have now.
There's nothing inherently wrong with using a deprecated method. If your AsyncTask is THAT critical that you can't cancel it and start it up again if your orientation changes, you should consider using a service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With