Every time the user presses a button, the following code is executed.
new Thread() {
@Override
public void run() {
aA = new albumArt(foo.getField("albumArt"),albumArt.GLOSSY_1, albumart,getBaseContext());
runOnUiThread(new Runnable() {
public void run() {
aA.placeAlbumArt();
}
});
}
}.start();
There’s a problem with this:
For example:
User presses the button => Image A starts to get downloaded.
User presses the button (before Image A has finished) => Image B starts to get downloaded.
Image B finishes (before Image A) to get downloaded and then it’s shown (with aA.placeAlbumArt()).
Image A finishes and replaces Image B.
How can I avoid that Image A replaces Image B if it finishes later?
I mean, is there any way I can cancel a Thread (Image A) if another one (Image B) gets started before the first one finishes?
That would also help not to queue a lot of clicks and doing the whole process uselessly. I mean, if the user presses the button before Image A has finished, there’s no point in finishing to process Image A. If the user presses the button 5 times before Image A finishes, there’s no point in downloading Image A, B, C and D. It should only download E (the fifth click).
If you were to use an AsyncTask then you could cancel it. You should be able to swap your Thread out for an AsyncTask without too much trouble.