1

After searching for a long , i went through the following links and understood ,

http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

AsyncTask never executes onPostExecute

But i want to do some API hits to the server and after getting all the data , i want to move to my mainactivity from the Splashscreen , below is my code . . . .

class Startsyntask extends AsyncTask<Void, Void, Void> 
    {

         @Override
         protected Void doInBackground(Void... arg0) 
         { 
            raw_data = new File(Environment.getExternalStorageDirectory() + "/.MyFoldernew");
            if(raw_data.exists())
            {
                Log.e("directory exists", " already ");
                new read_syntask().execute();
            }
            else
            {
                Log.e("directory created", " newly ");
                raw_data.mkdirs();
                new write_syntask().execute();
            }
            return null;    
         }

         @Override
         protected void onPostExecute(Void unused) 
         {
             if( i == 6)
             {
                finish();
                startActivity(new Intent("com.sample.app.Tabbar"));
             }
         }
    }

and in the code read_synctask and write_synctask are another asyncronous tasks that has some specific operations to do with , in these Asyncronous tasks it is calling the onPostexecute after doinBackground .

Actually it moves to the Tabbar Activity and the API hits continues and i get data read from server in Tabbar activity . how could i implement that only after completing the asyncronous task in doinBackground , onProgress should be called .

Community
  • 1
  • 1
VIGNESH
  • 2,023
  • 8
  • 31
  • 46
  • You can try to put the startActivity in the onPostExecute in the read_synctask and write_synctask as one of them has to complete before you navigate to the next activity. And WHY are you using two async tasks ? – Anukool Feb 14 '13 at 06:59

2 Answers2

1

Actually,

new read_syntask().execute();

and

new write_syntask().execute();

both have Asynchronous call which don't wait for complete the task and perform the next execution step, so your execution comes to onPostExecute() before finishes both AsyncTask.

What you have to do is,

use new read_syntask().execute().get(); and new write_syntask().execute().get();

Now get() method from AsyncTask will wait for complete the AsyncTask and then onPostExecute() will execute. (But I think it will block the UI thread).

Update: (Best approach)

Actually, there is no need of Startsyntask AsyncTask, You can directly write the if-else conditions of raw_data in Activity's method and also your code for starting Tabbar

finish();
startActivity(new Intent("com.sample.app.Tabbar"));

should be in either read_syntask or write_syntask 's onPostExecute().

user370305
  • 108,599
  • 23
  • 164
  • 151
  • @Anukool - yeah I knew, Both AsyncTasks are called in `doInBackground()`. And also to wait for complete Asynctask you have to use `get()` method. – user370305 Feb 14 '13 at 07:04
  • 1
    I am still unable to understand the use of two Multiple AsyncTasks. – Anukool Feb 14 '13 at 07:10
  • 1
    @Anukool - As its OP's Programming approach, we can't say anything without seeing entire code. We have to give answer, according to question and posted code. – user370305 Feb 14 '13 at 07:18
0

Quoting "how could i implement that only after completing the asyncronous task in doinBackground , onProgress should be called ."

I don't know much. But maybe using Handler can help. Send a message after the onPostExecute of the asynchronous task and on handleMessage of your handler, do what you want to do.

http://developer.android.com/reference/android/os/Handler.html

lorraine batol
  • 6,001
  • 16
  • 55
  • 114