Asynctask task only runs once, then does not arrive at theInBackground

Asked

Viewed 805 times

3

I am facing a strange problem in my application, since in I am testing in 4 different devices but only two of them have the expected behavior.

Scenario / Devices:

  • Nexus 4 / Android 5.0 - OK
  • Galaxy Tab 2 / Android 4.2.2 - OK
  • LG Optimus / Android 4.4.2 - Error
  • Genesis / Android 4.1.2 - Error

As you can see, 4 versions of Android different and only the first two are working.

The problem in question is that the first time I install the application on the device, I run a task with AsyncTask and it all happens normally. But from then on I can’t perform anymore.

As the method onPreExecute() runs in the same thread, man debug gets into it without problem, but not in the doInBackground().

I read something about the method executeOnExecutor() from API 11 (my application is from 15) but I don’t think that’s the case, since I don’t want several tasks in parallel but a simple.

The simple representation of my code is this:

new AsyncTask<Void, Void, Void>() {
    @Override
    protected void onPreExecute() {
        // Preparo minha UI
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Executo minha tarefa
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Conclusão
        super.onPostExecute(result);
    }
}.execute();

Anyway, I don’t know what might be happening, I’ve done a lot of research and found nothing. My development environment is the Android Studio 1.0.1, compiled with SDK version 21.

  • Only one doubt, where exactly you call the .execute()? Would be inside a onClick or in the onCreate?

  • I have tasks in the onCreate of ativity and onClick of a button too, but in all of them the same problem occurs, after working the first time after an installation, no longer works.

  • You mean you use the same AsyncTask for various tasks? Let me get this straight, assuming you’ve created an event onCreate and he calls the AsyncTask, if clicking the first time works, but if clicking the second does not work? Or you mean that it only works in the first installation?

  • Another doubt, you really need to use return null; in a method that uses Void?

  • It’s not the same AsyncTask because for each one I create a new instance. I have a task to find the location on onCreate of Activity and a button to send that location for example. In the first installation, the first task of finding the location works. Then I can’t even finish the task of onCreate again and not even click the button. Somehow the thread it seems like it’s being busy, keeping others from running, but I have no idea how this is happening, as you can see, my code is pretty simple.

2 answers

2


@Paulorodrigues believe that for not being "bound" Thread maybe the process is busy. One way to do it would be to "extend" the AsyncTask in its main class.

Another thing I noticed is that you used return null; in a method that returns Void, I believe this is not the correct (or necessary) and you also used Void in a type of method, usually it would be "lowercase" (minuscule letter):

protected void doInBackground(Void... params) {

The code should be like this:

public class AsyncTaskActivity extends Activity {
    ...

    public void onClick(View view){
            new meuMetodo().execute();
    }

    private class meuMetodo extends AsyncTask<Void, Void, Void> {
        ....
        @Override
        protected void doInBackground(Void... params) {
            // Executo minha tarefa
        }
        ....
    }
    ...
}
  • William, I have transferred all the new AsyncTask... that I made directly in my calls to an extended class of AsyncTask and it worked. I don’t understand why this happened, I thought it wasn’t necessary to have a class just to do in another thread, and stranger, in 4 versions of Android there were distinct behaviors.

  • Regarding the other point, it is not possible to pass void in class AsyncTask, so I can’t make the return of doInBackground() be it void so of the Void object and the null return, never had problem. But anyway, your first tip has solved my problem. Thank you!

  • @Paulorodrigues believe that the AsyncTask need the main class to receive the "Active" "signals". I’m glad it worked, success in your project!

0

Friend, try not to use the same object for each run.

Every time you run Asynctask Guide, instate a new object.

I believe that way it will work normally.

I’ve had similar problems and, researching, I solved it this way.

Don’t do this:

 AsyncTask obj = new AsyncTask();

    obj.execute();

Depois novamente ...


 obj.execute();

Every time you run, do:

new AsyncTask().execute();

Considering the appropriate parameters for your case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.