Note that there are some explanatory texts on larger screens.

plurals
  1. POAsyncTask doInBackground does not run
    primarykey
    data
    text
    <p>I'm having a problem with the AsyncTask class. It seems like my task stops working after creating 4 or 5 tasks. </p> <p>Im having 2 activities. MainActivity which only holds a button that starts a second activity called ImageActivity.</p> <p>ImageActivity is very simple. it got an onCreate that sets the layout, and then it starts a new AsyncTask that loads an image from the internet. This works fine the first few times. But than it suddenly stops working. The onPreExecute method is run every time, but not the doInBackground method. I have tried to simplify the doInBackground with a sleeping loop, and the same thing happens. I cant understand this behavour since the asynctask is both canceled and set to null in the onDestroy method. So every time i start a new ImageActivity, i also create a fresh AsyncTask.</p> <p>I recreate the ImageActivity and the task by hitting the back button, and than clicking the button on the MainActivity.</p> <p>Any ideas anyone? I'm really struggling with this one.</p> <p>UPDATE: Code that starts the ImageActivity (inside a button onClickListener)</p> <pre><code>Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.setClassName(this, ImageActivity.class.getName()); startActivity(intent); </code></pre> <p>The code above starts this activity</p> <pre><code> public class ImageActivity extends Activity { private AsyncTask&lt;Void, Void, Void&gt; task; public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.main); task = new AsyncTask&lt;Void, Void, Void&gt;() { @Override protected void onPreExecute() { Log.d(TAG, "onPreExecute()"); } @Override protected Void doInBackground(Void... params) { Log.d(TAG, "doInBackground() -- Here is the download"); // downloadBitmap("http://mydomain.com/image.jpg") return null; } @Override protected void onPostExecute(Void res) { Log.d(TAG, "onPostExecute()"); if(isCancelled()){ return; } } }.execute(); } @Override protected void onDestroy() { super.onDestroy(); task.cancel(true); } } </code></pre> <p>UPDATE:</p> <p>I have tested using a combination of traditional Threads and runOnUiThread method, and it seems to work better. Now the thread runs every time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload