AsyncTask with screen rotation - onRetainNonConfigurationInstance deprecated

I'm still trying to find the β€œright” design pattern when working with AsyncTask and screen rotation. I read this commonsware community post that references this code , but the onRetainNonConfigurationInstance method is now deprecated. The magic sentence in the documentation reads: "Ensuring that messages are not processed during the transition to the next activity makes it easier to use with active objects ...", which is a really important part of what this method did. I see no suggestion to use setRetainInstance () to accomplish the same goal.

I also saw messages such as this one , whose number one is really nothing but a bad hack that covers only 90% of use cases (that is, what happens if your task is started and then a phone call arrives, this solution will not work) .

It was suggested that you review the code here for AsyncTask examples, but if I'm not dense, I don’t see that they are used anywhere in the application (when using the search function in google code)

This question has obviously been asked many times, but I have not seen an updated, proper asnwer. If one exists, answer and close it as a duplicate if you want, but at least answer! :)

This is another link that addresses the same, deprecated method.

+6
source share
2 answers

It is only deprecated on Honeycomb and above, and it will work just fine on them. The "new" way is to use downloaders (you can use the compatibility library to get them in versions prior to HC) or saved snippets. If you call setRetainInstance() , then the instance is passed as is for the newly created action (they actually use the onRetainNonConfigurationInstance in the FragmentActivity the compatibility library), so it is effectively similar to what you have now.

+1
source

There is nothing wrong with using an obsolete method. If your AsyncTask is EXACT, that you cannot cancel it and start it again, if your orientation changes, you should consider using the service.

+1
source

Source: https://habr.com/ru/post/900984/


All Articles