AsyncTask is deprecated? What method used instead of onPreExecute and onPostExecute?
Asked Answered
D

2

6

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();

    }
}
Digest answered 11/11, 2020 at 11:56 Comment(6)
I've answered similar question few days ago https://mcmap.net/q/1769645/-how-to-run-a-algorithm-on-a-separate-thread-while-updating-a-progressbarSnafu
@Snafu In "long running operation " we paste doInBackground code and "Update ui on the main thread" paste onPreExecute right?Digest
then where we paste onPostExecute code?Digest
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.Snafu
use RXJava or coroutine in kotlin.Connie
There is no simple answer to your question. You should use rxJava or kotlin coroutines together with a right architecture approach like clean architectureAdriatic
S
8

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
    });

  });   
Snafu answered 11/11, 2020 at 13:6 Comment(2)
I have one class which have AsyncTask like this - new AsyncTask<Integer, Void, Float>() {...} means directly, it's not like i'm calling it from somewhere, so what about params from doInBackground??Python
Here's link for more help - github.com/sharish/ScratchView/blob/master/views/src/main/java/…Python
S
6

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

Snowfield answered 11/11, 2020 at 12:30 Comment(5)
thank you for your response. Can't understand. can u post any sample code or modify my above code?its helpful to us @SnowfieldDigest
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 ;-)Snowfield
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()Headspring
Yes. No need to use runOnUiThread() in the constuctor of your Thread.Snowfield
Just sad. they should have provided an easy alternative. async task is fairly convenient. it even has status and cancelHeadspring

© 2022 - 2024 — McMap. All rights reserved.