Java Android AsyncTask - When does AsyncTask Stop Working?

I have an AsyncTask that starts and does its job and shuts down. onPostExecute does what it should do, and all is well. BUT this task is still running in the debug window in Eclipse. Every time I run a task, I get a new one. So this is not a reuse of the task, I think.

Has anyone seen this before?

+4
source share
4 answers

I wrote this test application to try asynctask. it starts 5 background tasks and then has to reuse them. I just would like to know that everything is in order, since I could not find links to this problem. I would like to know why 5 tasks? Do they free up memory and all that they take? I would put this in a comment, but didn't seem to allow the code

package com.mvw.tas; import java.io.IOException; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class TestAsync extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnD = (Button)findViewById(R.id.Btn01); btnD.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { new gback().execute( ""); } }); } //////////////////////////////////////// public class gback extends AsyncTask<String, Void, IOException> { //@Override protected IOException doInBackground(String... params) { Log.d( "ASTEST", "Running background"); return null; } @Override protected void onPostExecute(IOException result) { Log.d( "ASTEST", "Post Exec"); } @Override protected void onPreExecute() { Log.d( "ASTEST", "PRE Exec");; } } } 

and XML

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="Test AysncTask" android:id="@+id/Btn01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> 
0
source

This is not a reuse of a task. It continues to work until onPostExecute ends. It is probably still in your debug window because the object has not yet been garbage collected. Do not worry about it.

+1
source

Why should you reuse tasks? The javadoc for AsyncTask is pretty clear: we should not reuse AsyncTask as soon as doInBackground () starts before completion. Attempting to reuse will result in an exception.

Exact words from Javadoc: "A task can only be completed once (an exception will be thrown if a second execution is attempted).

+1
source

Any objects listed in the debugger will not collect garbage. This is because the debugger contains a reference to them and is the correct behavior

0
source

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