4

In my old project, I used AsyncTask, but its deprecated so what method i used instead of this? If we used thread, in AsyncTask having onbackground, onPreExecute and onPostExecute Override methods where this sections called in thread. Is there any alternative method. Can you please give me the solution?

Java code:

      public class getDetailsfromWeb extends AsyncTask<Void, Void, String> {

      @Override
      protected String doInBackground(Void... params) {

        if (paymentSync == null)

            paymentSync = new ReceivePaymentSync(getActivity());

        allCreditCardModel = new AllCreditCardModel();

        allCreditCardModel = paymentSync.getGetCreditCardById(CrediCardId);

        handler.post(allCreditRunnable);

        return null;
    }

    /**
     * @param string
     */
    public void execute(String string) {

        // TODO Auto-generated method stub
    }

    protected void onPreExecute() {

        showProgress();

    }

    @Override
    protected void onPostExecute(String result) {

        progress.dismiss();

    }
}
Ana
  • 174
  • 1
  • 2
  • 10
  • I've answered similar question few days ago https://stackoverflow.com/a/64619184/14507326 – zjmo Nov 11 '20 at 12:06
  • @zjmo In "long running operation " we paste doInBackground code and "Update ui on the main thread" paste onPreExecute right? – Ana Nov 11 '20 at 12:12
  • then where we paste onPostExecute code? – Ana Nov 11 '20 at 12:12
  • Depends, maybe in your case pre execute goes on the mainthread, then open the rmthread as shown in the answer, execute your heavy code in background and after put the code for posting on the mainthread from within the background thread. Actually you can post on the mainthread as many time as you want. – zjmo Nov 11 '20 at 13:00
  • use RXJava or coroutine in kotlin. – Abhay Koradiya Nov 11 '20 at 13:07
  • There is no simple answer to your question. You should use rxJava or kotlin coroutines together with a right architecture approach like clean architecture – Beloo Nov 11 '20 at 13:08

2 Answers2

6

This is a simple example, anyway I would give a look to the WorkManager library too:

Executors.newSingleThreadExecutor().execute(() -> {

    Handler mainHandler = new Handler(Looper.getMainLooper());

      //sync calculations

    mainHandler.post(() -> {
      //Update UI
    });

    //other sync calcs.

    mainHandler.post(() -> {
      //Update UI
    });

  });   
zjmo
  • 625
  • 2
  • 8
  • I have one class which have `AsyncTask` like this - `new AsyncTask() {...}` means directly, it's not like i'm calling it from somewhere, so what about `params` from `doInBackground`?? – Vivek Thummar Oct 06 '21 at 06:51
  • Here's link for more help - https://github.com/sharish/ScratchView/blob/master/views/src/main/java/com/cooltechworks/views/ScratchTextView.java#L317 – Vivek Thummar Oct 06 '21 at 07:14
5

Just use a Thread.

The onPreExecute code goes in the constructor.

The doInBackground code goes in the run().

The onPostExecute code goes in the run of a runnable for runOnUiThread which you call at the end of the run().

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • 3
    thank you for your response. Can't understand. can u post any sample code or modify my above code?its helpful to us @blackapps – Ana Nov 11 '20 at 12:32
  • If you google for a Thread example and then for runOnUiThread you will find all you need. There is not much in it. Then post the code you tried if it does not work and we will talk about it. Today or tomorrow ;-) – blackapps Nov 11 '20 at 12:36
  • So the code in the constructor won't crash if i do some ui work like in post execute? i cant use runOnUiThread inside the constructor, since i also have to call that in the run() – chitgoks May 03 '21 at 11:49
  • Yes. No need to use runOnUiThread() in the constuctor of your Thread. – blackapps May 03 '21 at 12:26
  • Just sad. they should have provided an easy alternative. async task is fairly convenient. it even has status and cancel – chitgoks May 05 '21 at 11:56