0

I'm using AsyncTask() to communicate with the server , and I'm getting and answer from it every 3 seconds .

I know that when I close my app , the communication is still going because I'm using a Toast to popup the answer. The only way is to force the close of the app . Is there a way to fully close it ?

Thanks!

Escaga Seen
  • 131
  • 11
  • Cancel `AsyncTask` . Provide code if you need you need a proper solution – ADM Jun 14 '18 at 16:49
  • There are several examples of this on SO :: https://stackoverflow.com/questions/28393636/how-to-kill-a-activity-and-the-asynctask :: https://stackoverflow.com/questions/10882543/how-to-completly-kill-remove-delete-stop-an-asynctask-in-android – Barns Jun 14 '18 at 16:54
  • I want to cancel the AsyncTask but only when I 'close the app' , is there such a thing as a code to detect when I close the app so I can implement the method to cancel the async task ? – Escaga Seen Jun 14 '18 at 17:07

3 Answers3

3

Assuming you are calling AsyncTask.cancel() when onDestroy() is called.

I will suggest you to keep a variable to know if you are using AsyncTask.Use this variable to decide when to cancel() the task.

If task is not running cancel on onStop(), Else task is running, then on onDestroy().

And remember to cancel() and release all the resources when onDestroy() is called.

Amrit kumar
  • 466
  • 6
  • 9
  • I'm only doing the Toast to prove myself that it is still runnig. Because it's not efficient when you close an app and it is still doing connections in background. But I'll try what you said and give you the feedback . Thanks for the answer! – Escaga Seen Jun 14 '18 at 16:59
1

enter image description here

Depends on what you mean when you say "close". When you close the app you mean suspend it into the background? or EXIT. If you want to stop the async task while the app is in the background call on onStop() and continue it with onResume() when it's brought back up. If you mean EXITing the app by app switcher and force closing use onDestory();

Here is a link to the api article that explains this in detail. It's called activity lifecycle and it's vital to understand it's concepts to program an android app.

https://developer.android.com/guide/components/activities/activity-lifecycle

So in onStop override you would do something like yourAsyncTaskObj.cancel(true);

@Override
protected void onStop() {
    // place your code before super.onStop()
    yourAsyncTaskObj.cancel(true);
    super.onStop();

}
Kapta
  • 1,525
  • 14
  • 24
1

Consider it to make generic in whole app .you should implement Application.ActivityLifecycleCallbacks in Application class and avoid all leaks based on Activity lifecycle.

@Override
    public void onActivityResumed(Activity activity) {
      Log.d("ActivityResumed");
    }

@Override
public void onActivityPaused(Activity activity) {
       Log.d("ActivityPaused");
}

@Override
public void onActivityStopped(Activity activity) {
       Log.d("ActivityStopped");
}

And cancel your background task based on your requirement .

Also i would recommend you to use Retrofit ,Volley or any other good 3rd party networking library to avoid similar other situations .it makes really easy to manage code .

Godfather
  • 833
  • 9
  • 14