How do I cancel Asynctask to continue running after exiting an activity?

Source: Internet
Author: User

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

  1. Set activity singletask to prevent creating a new object when an activity is started

    1. Use Asynctask when the activity is destroyed. The method that was canceled

    2. Write yourself a Asynvtask

  2. 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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.