Android: Pressing the back button when starting AsyncTask

I have an ActivityA that has a button. After clicking the button , another ActivityB opens.

onCreate() method of ActivityB , I run AsyncTask , which connect to the server and extract the results and display it on the screen.

Here is the Activity B code:

 public class ActivityB extends Activity { AsyncFetch asyncFetch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(null != savedInstanceState) { return; } asyncFetch = new AsyncFetch(); asyncFetch.execute(); } } private class AsyncFetch extends AsyncTask<Object, Void, Object> { @Override protected void onPostExecute(Object o) { } @Override protected Object doInBackground(Object... params) { //contacts the http server. } } 

When executing AsyncFetch , when I click the button, click ActivityB not immediately. But will be destroyed upon completion of AsyncTask .

How do I implement this to avoid repeated freezing? I tried below:

 @Override protected void onDestroy() { asyncFetch.cancel(true); super.onDestroy(); } 

But still, he is waiting for AsyncTask cancel or terminate.

Like a GMail app when I open something. Although in the process, if I click the back button, it just exits this Activity .

Is there any other way to implement the background process? Any design pattern to implement this? Please let me know.

Any help is appreciated.

+6
source share
5 answers

Just treat yourself with the onBackPressed () method:

 @Override public void onBackPressed() { if (asyncFetch != null) asyncFetch.cancel(true); super.onBackPressed(); } 
+4
source

From the Android developer website :

Cancel a task

A task can be canceled at any time by calling cancel (boolean). Calling this method will result in subsequent calls to isCancelled () to return true. After calling this method, onCancelled (Object) will be called instead of onPostExecute (Object) after doInBackground (Object []) returns. To ensure that the task is canceled as quickly as possible, you should always check the returned value of isCancelled () periodically from doInBackground (Object []), if possible (inside a loop, for example.)

So, you can mark AsyncTask as canceled in onPause() or onBackPressed() from your Activity , and then you can add several if blocks to doInBackground() and return immediately if the task is canceled.

For instance:

 @Override protected void doInBackground(Object... params){ // do some work if(isCancelled()){ // end task right away return null; } // if task is not cancelled yet, // do some other work } 

However, if a network operation is already running, you need to cancel it from HttpClient .

+3
source

Try the following:

 @Override public void onBackPressed() { asyncFetch.cancel(true); // If you want to finish the activity you can use below code // finish(); } 
+2
source

Here we go. Try it.

 @Override public void onBackPressed() { if (asyncFetch.getStatus() == AsyncTask.Status.RUNNING) { asyncFetch.cancel(true); } finish(); } 
0
source

When you click back button , it will first be called onBackPressed() , so in this part, cancel the asynctask .

 @Override public void onBackPressed() { if ( asyncFetch != null && asyncFetch.getStatus() == AsyncTask.Status.RUNNING ){ asyncFetch.cancel(true); } } 
-1
source

Source: https://habr.com/ru/post/976857/


All Articles