Problem
When entering a detailed page, the program will pop up a dialog box to load the network data, but found that users often because of slow loading of data, quickly exit the page, so repeatedly back and forth several times, found that Asynctask no longer continue to load, but slowly wait, check the next number, is the first few not timely close, Causes the current asynchronous task to wait.
So I want to ask how do I close the corresponding asynchronous task at the same time after exiting a page?
Initial solution to the code scenario:
public class Task extends Asynctask<void, Void, void>{
@Override
protected void Doinbackground (void ... path) {
The task is canceled, exit the loop now
if (iscancelled ()) return null;
}
@Override
public void Onprogressupdate (File ... files) {
The task is canceled and the subsequent code is no longer executed
if (iscancelled ()) return;
.........
}
}
UI thread:
Keep a reference to a 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 the task is still running, cancel it first
}
}
}
The theory and explanation of the solution
Set activity singletask to prevent creating a new object when an activity is started
Use Asynctask when the activity is destroyed. The method that was canceled
Write yourself a Asynvtask
Comments by Netizens:loading network data, accessing databases, files, and so on, should be another thread and run in the background without the user waiting for your data to load.
to get back to your question, Asynctask is using a thread pool, which is not put back into the pool. A new asynctask will take out the thread and start executing, which is why you have multiple asynctask. So even if you call Asynctask's Cancle method, you'll find that it still doesn't end there.
you try to get the Asynctask object that is bound to the current interface while closing the interface, and set it to empty. If that doesn't work, consider encapsulating a download thread yourself.
The code after the last debug
public class LoadPage extends Asynctask<void, Void, void> {
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);
}
}
Summarize:
After the above analysis and the specific suggestions given by netizens, has solved the problem, in this, thinking about the problem at the same time, can not forget how to better from the theory, thank the technical people's advice, technical people's life, technology to create the future!
How do I cancel Asynctask to continue running after exiting an activity?