Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report