Android: override onPostExecute() if AsyncTask.execute(new Runnable()) is invoked in a method

Related searches

I have a method which I want to execute on a background thread:

public class MainActivity extends AppCompatActivity {
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        foo();
    }

    public static void foo(){
        AsyncTask.execute(() -> {
            // perform some object creations
        });
    }

    public static void foo2(){
      // Method that updates UI (lists and views) which requires the main thread.
    }
}

I would like to execute foo2 when AsynTask is complete i.e. onPostExecute(). How can I override onPostExecute() in this case?

You cant achieve directly ,You can do through extend the Async class.

AsyncTask, and most often will override a second one ( onPostExecute(Result) .) Params , the type of the parameters sent to the task upon execution. Invoking this method will cause subsequent calls to isCancelled() to return Creates a new asynchronous task. public static void execute (Runnable runnable). Override this method to perform operation on a background thread. onPostExecute: void onPostExecute (Result result) Runs after doInBackground method completes its processing.

It may be helpful, You can achieve this by class instead of method. Please try the below snippet:

@Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      foo asyncTask=new foo();
      asyncTask.execute();

   }

private class foo extends AsyncTask<void, void, void> {
      @Override
      protected void onPreExecute() {
         super.onPreExecute();

      }
      @Override
      protected void doInBackground(String... strings) {

      }
      @Override
      protected void onPostExecute() {
         super.onPostExecute();
         foo2 asyncTask=new foo2();
         asyncTask.execute();
      }
   }

private class foo2 extends AsyncTask<void, void, void> {
      @Override
      protected void onPreExecute() {
         super.onPreExecute();

      }
      @Override
      protected void doInBackground(String... strings) {

      }
      @Override
      protected void onPostExecute() {
         super.onPostExecute();

      }
   }

AsyncTask, AsyncTask enables proper and easy use of the UI thread. and most often will override a second one ( onPostExecute(Result) .) Invoking this method will cause subsequent calls to isCancelled() to return true. Creates a new asynchronous task. Convenience version of execute(Object) for use with a simple Runnable� The following examples show how to use android.os.AsyncTask#execute() .These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

It's Work For me

public class MyTask extends AsyncTask<String, Void, String>
{
    @Override
    protected Void doInBackground(String... params) {
        //Do background 
    }

    protected void onPostExecute() {
        foo2();
    }
}

void foo2(){
    //your function
}

If you want to perform some function once the Asynctask is complete, You can also update the UI from within onPostExecute, which is simpler if it works for you.

Android AsyncTask Example Tutorial, Hence use of AsyncTask in android application keeps the UI thread responsive at all onPostExecute() : This method is called after doInBackground method� new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { // .. some UI updates } }); this must have overloaded main thrad message queue, and caused long delay before onPostExecute would get called. The solution was to post only once every second.

Introduction to background processing in Android - Tutorial, If you need to update the user interface from a new Thread , you class can be an instance of the Message or the Runnable class. To implement a handler subclass it and override the handleMessage() method to process messages. By default, AsyncTask tasks are executed sequence (for Android� Override the onPostExecute method and perform the tests there. To wait for the AsyncTask to complete use CountDownLatch. The latch.await () waits till the countdown runs from 1 (which is set during initialization) to 0 (which is done by the countdown () method).

core/java/android/os/AsyncTask.java, <p>AsyncTask was intended to enable proper and easy use of the UI thread. However one method ({@link #doInBackground}), and most often will override a if (isCancelled()) break;. * } new DownloadFilesTask().execute(url1, url2, url3); <li>{@link #onPostExecute}, invoked on the UI thread after the background. Because in lower versions of Android all AsyncTasks were executed at single background thread. So new tasks might be waiting, until other task working. In lower versions in Android (actually on pre-HONEYCOMB) you can't execute AsyncTask on executor. Change your code to

Android AsyncTask. Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive.

Comments
  • Using AsyncTask.execute(Runnable) you can't override onPostExecute. You should create a new AsyncTask or create your class which extends AsyncTask.