What are the advantages and disadvantages of asynctask in the Java Android framework?

I'm learning Android application development from udacity.com by Google engineers. They say,

"Using 'asynctask' is not a good idea because it is not attached to the activity life cycle. As long as asynctask is running, the virtual machine will keep the active object, even if Android calls the ondestroy() method. The activity and wants to discard it

If you rotate the phone, the behavior is to destroy the current activity and instantiate a new activity. Now, two threads in the naive asynctask implementation try to perform the same update. Therefore, this is not the best mode for background operations that may take a long time to run (such as getting from web services). If you leave the application, as long as the process remains active, Asynctask will run, but it will run at a lower priority. If the device needs more resources, your process will be the first thing to be killed. "

1) If using asynctask is bad, why create it? Despite having services (or similar services that implement similar functions), why did the design philosophy create it?

2) Under what circumstances should asynctask be used for improvement over the services / similar options available in Android?

3) Where should asynctask not be used?

Please don't object to this problem. I searched stackoverflow, but I couldn't find a similar problem

resolvent:

Benefits of asynctask

>Provide a common solution for all network calls > publish progress to UI during execution. > asynchronous operation > easy to maintain and read

Problems in aysnctask

>When rotating the screen, the activity is destroyed, so asynctask will not have a valid reference to publish data from onpostexecute(). In order to retain it, setretainstate (true) needs to be used if it is called from fragment; If you call from the activity method of the activity, you need to use onconfigchanges(). > if the activity is completed, the execution of asynctask will not be cancelled automatically. You need to cancel them, otherwise they will continue to run in the background. > If any exceptions occur during the execution of network tasks, you need to handle them manually

Asyctask, services, intentservice and threads all run on different threads and have different purposes. Please read more details here

Therefore, you need to determine which component to use when performing non - UI operations

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>