I have noticed this in the last few months. If I set clickListener to a button to launch AsyncTask, it will not always be executed. I solve the problem by wrapping a line of code that executes the AsyncTask method in the method.
Here is the code that does not work all the time:
button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new CustomAsyncTask().execute(); } });
Here is my fix:
button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { executeAsyncTask(); } }); public void executeAsyncTask(){ new CustomAsyncTask().execute(); }
I didnโt notice the pattern when it does it and it wonโt break, but I wonder if anyone else noticed this?
Also, if someone has an explanation why this is happening, that would be cool too.
Thanks in advance.
EDIT
Here's CustomAsyncTask (I know that it's not called all the time because it doesn't print anything)
private class CustomAsyncTask extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... params) { Log.i("CustomAsyncTask", "doInBackground"); return null; } }
source share