1

Here's my code in my main thread

            myAsyncRunnable mar = new myAsyncRunnable(GameActivity.this);
            mar.execute("fire");
            PhotoTask photoTask = new PhotoTask(camera,surfaceCamera,isPreview,holder,GameActivity.this);
            photoTask.execute(null);

in myAsyncRunnable.fire() I have a loop that changes 10 times the image of an ImageView in gameActivity. I want the photoTask to start when the last image has been changed

@Override
protected Void doInBackground(String... params) {
fire();
return null;
}

public void fire() {
        final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
        final int drawables[] = new int[] {R.drawable.fire1,R.drawable.fire2,R.drawable.fire3,R.drawable.fire4,R.drawable.fire5,R.drawable.fire6,R.drawable.fire7,R.drawable.fire8,R.drawable.fire9,R.drawable.fire10,R.drawable.fire11,R.drawable.fire12,R.drawable.fire13,R.drawable.fire14,R.drawable.fire15,R.drawable.fire16};
        for (int i=0;i<drawables.length;i++) {
            final int j=i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    image3.setImageResource(drawables[j]);
                }
            };
            gameactivity.handler.postDelayed(runnable, 200*j);
        }
    }

But photoTask executes before the last image has apperaed. This is probably due to the postDelayed method and I have tried to execute photoTask in the method onPostExecute() but it didn't work either

morg
  • 1,173
  • 4
  • 18
  • 36

2 Answers2

1

Put photoTask.execute(null); in the onPostExecute() of the first AsyncTask

pvn
  • 2,016
  • 19
  • 33
  • I have tried that (see my initial message) but it didn't work either as I said – morg Feb 11 '13 at 12:47
  • What happpened when you started 2nd from onPost() of first? It should definetly work as onPost() is called only afer doInBackground() has completed! – pvn Feb 11 '13 at 12:50
0

Just as pvn said, you need to override the onPostExecute method too and execute your photo async class there. Put this after the doInBackground method.

@Override
protected void onPostExecute(YourParam param)
{
     PhotoTask photoTask = new PhotoTask(camera,surfaceCamera,isPreview,holder,GameActivity.this);
        photoTask.execute(null);
}
Rockbag
  • 78
  • 9