Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask with screen rotation - onRetainNonConfigurationInstance deprecated

I am still trying to find the "correct" design pattern when dealing with AsyncTasks 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.

like image 666
Ryan Avatar asked Nov 08 '11 00:11

Ryan


2 Answers

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.

like image 67
Nikolay Elenkov Avatar answered Sep 30 '22 14:09

Nikolay Elenkov


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.

like image 44
Falmarri Avatar answered Sep 30 '22 14:09

Falmarri