Jack
Jack

Reputation: 2063

Strange AsyncTask Behavior

I have successfully set up 2 items on the action bar to open up 2 seperate Async Tasks to perform different tasks in the background.

My first item launches an AsyncTask class that will only download the images and save them to the SD card.

My Second item launches an AsyncTask class that will download the image, then set it as the users wallpaper.

The issue that I'm having is that when the second item is pressed(Setwallpaper), it will do the 2 tasks at the same time that have been separated by the 2 classes(Save and set the users wallpaper).

Then, when I remove the other launch code(setwallpaper/vice versa) it will just do the one task, not 2 at the same time.

I have no idea why this behaving like this, would someone help me out. Thanks...

Code:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.test, menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.SaveWallpaper:

        new SaveWallpaperAsync(getActivity()).execute(mImageUrl);

        return true;



    case R.id.SetWallpaper:

        new SetWallpaperAsync(getActivity()).execute(mImageUrl);

        return true;
    }
    return super.onOptionsItemSelected(item);
    }  

Upvotes: 1

Views: 62

Answers (2)

sulai
sulai

Reputation: 5364

Try something like this:

class SaveWallpaperAsync extends AsyncTask {

    doInBackground(...) {
        image = Utils.downloadImage(...);
        Utils.saveToSdCard(image);
    }

}

class SetWallpaperAsync extends AsyncTask {

    doInBackground(...) {
        image = Utils.downloadImage(...);
        Utils.setBackground(image);
    }

}

Upvotes: 2

Ismail Sahin
Ismail Sahin

Reputation: 2710

There is no break between cases :) while return should do the same think, you should try using break after return

Upvotes: 1

Related Questions