Get the App
SLTechnology News&Howtos  ›  Development  › 

How to cancel the AsyncTask to continue running after exiting an activity

Shulou Source: shulou.com Published: 2022-06-02 10:23:42 09月22日 Update

This article will explain in detail how to cancel the AsyncTask to continue to run after quitting an activity. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

problem

When entering a detailed page, the program will pop up a dialog box to load network data, but find that users often exit the page quickly because of slow data loading. After going back and forth several times, they find that AsyncTask no longer continues to load, but slowly wait, check the next number, the first few are not closed in time, resulting in the current asynchronous task has been waiting.

So I'd like to ask how to close the corresponding asynchronous task after exiting a page?

Preliminary code solution:

Public class Task extends AsyncTask {

@ Override

Protected Void doInBackground (Void... Path) {

/ / Task has been cancelled. Exit the loop immediately.

If (isCancelled ()) return null

}

@ Override

Public void onProgressUpdate (File... Files) {

/ / Task has been cancelled and the following code will no longer be executed.

If (isCancelled ()) return

.

}

}

UI thread:

/ / keep a reference to Task

Private PhotoTask task

/ / 1, start a new task

Task = new PhotoTask ()

Task.execute (path)

/ / 2, cancel the task

If (task! = null & & task.getStatus () = = AsyncTask.Status.RUNNING) {

Task.cancel (true); / / if Task is still running, cancel it first

}

}

}

The theory and explanation of the solution

Set Activity SingleTask to prevent a new object from being created when an Activity is opened

Use AsyncTask. Activity when destroying it. The way to cancel

Write your own AsynvTask.

Netizens' comments: loading network data, accessing databases, files and other problems, you should set up another thread and run in the background, do not let users wait for your data to load.

Coming back to your question, AsyncTask uses a thread pool, and threads are put back into the pool if they are not used. A new AsyncTask will fetch existing threads and start execution, which is why you have multiple AsyncTask. So even if you call the cancle method of AsyncTask, you will find that it still doesn't end.

You try to get the AsyncTask object bound by the current interface while closing the interface and set it to empty. If it still doesn't work, you can consider encapsulating your own download thread.

The final debugged code

Public class LoadPage extends AsyncTask {

Private volatile boolean running = true

Private final ProgressDialog progressDialog

Public LoadPage (Context ctx) {

ProgressDialog = new ProgressDialog (ctx)

ProgressDialog.setCancelable (true)

ProgressDialog.setOnCancelListener (new OnCancelListener () {

@ Override

Public void onCancel (DialogInterface dialog) {

Running = false

}

});

}

@ Override

Protected void onPreExecute () {

ProgressDialog.show ()

}

@ Override

Protected void onCancelled () {

Running = false

}

@ Override

Protected Void doInBackground (Void... Params) {

While (running) {

/ / does the hard work one,two,three

Log.e ("tag", "run..")

}

Log.e ("tag", "exit")

Return null

}

/ /...

@ Override

Protected void onPostExecute (Void result) {

Super.onPostExecute (result)

}

}

On how to quit an activity after a good cancellation of AsyncTask to continue to run to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Thread data run task code problem page no content at the same time object article scheme method time more user interface knowledge article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Xiaomi Docker MySQL