onPostExecute executes before doInBackground in Asyncronous task

一个人想着一个人 提交于 2020-01-05 04:52:06

问题


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 .


回答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().




回答2:


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



来源:https://stackoverflow.com/questions/14869347/onpostexecute-executes-before-doinbackground-in-asyncronous-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!