It is easy to maintain progress bar state when i use AysncTask with fragments Callback but how should i achieve it with volley? I can't use AsyncTask because it is outdated and volley is better and faster. Any Help or Hint will be grateful.
I am using google's volley to Post and Get Requests
Sure, here is how to maintain the progress bar state using Volley Android:
Code snippet
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
public class ProgressDialogHandler {
private ProgressDialog progressDialog;
private Handler handler;
public ProgressDialogHandler(Context context) {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
handler = new Handler(Looper.getMainLooper());
}
public void show() {
progressDialog.show();
}
public void dismiss() {
progressDialog.dismiss();
}
public void setProgress(int progress) {
handler.post(() -> progressDialog.setProgress(progress));
}
}
This code creates a ProgressDialogHandler class that can be used to maintain the state of a progress bar. The class has three methods:
show(), dismiss(), and setProgress(). The
show() method shows the progress dialog, the dismiss() method dismisses the progress dialog, and the
setProgress() method sets the progress of the progress dialog.
To use the ProgressDialogHandler class, you would first create an instance of the class. Then, you would call the
show() method to show the progress dialog. Once the progress dialog is shown, you can call the
setProgress() method to update the progress of the progress dialog. Finally, you would call the
dismiss() method to dismiss the progress dialog.
Here is an example of how to use the ProgressDialogHandler class:
Code snippet
ProgressDialogHandler progressDialogHandler = new ProgressDialogHandler(this);
progressDialogHandler.show();
// Do some work that takes some time...
progressDialogHandler.setProgress(50);
// Do some more work that takes some time...
progressDialogHandler.setProgress(100);
progressDialogHandler.dismiss();
In this example, the progress dialog is shown and the progress is updated to 50%. Then, some work that takes some time is done. After the work is done, the progress is updated to 100% and the progress dialog is dismissed.
This is comparing apples and oranges. They both use threads. Volley threads are not any faster than the threads in async task. The queues are separate but that is about it. In API 11 & higher you are allowed to use your own threadpool for AsyncTask instances.
Second, define better.
Volley is designed for sending a lot of light payloads (GET/POST) to a server and getting back pretty quick responses. These responses can then be used by the caller.
AsyncTask is designed to complete a given task off the UI thread and provide various callbacks as to the state of that task.
For your ProgressBar I am assuming you are either trying to determine the progress of a request that is being executed. In the Volley world since these are expected to be tiny, you have pretty much 3 states. Not Started, Executing(also contains start parsing) and Done (comprised of success, error and cancelled and such). As you know with AsyncTask there is a callback for onProgress when using publishProgress. So your instance can define anything it wants to send through as an indication of progress.
If your payload is big and will take time to transfer to the server, Volley may not be appropriate. Volley doesn't do a great job or even try to do a great job of sending large payloads to and from a server. The reason is that this just isn't what it is meant for. Like it requires that all payloads, upload and receive can fit in memory entirely. So If you have a few volley requests going all over, and each one with like a 1MB payload and a 1MB response, you could see this adding up pretty quickly.
Volley is great library but consider what it is recommended to be used for. Read the documentation and implementation of the code for more info.
If you are doing something that is going to take a rather long time, I would write a specific request type that sends and streams content to and from. That way you can tell how much work is left with the request. I am assuming you are using bytes sent and receive as the measure for progress.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is how to maintain the progress bar state using Volley Android:
Code snippet
This code creates a
ProgressDialogHandlerclass that can be used to maintain the state of a progress bar. The class has three methods:show(),dismiss(), andsetProgress(). Theshow()method shows the progress dialog, thedismiss()method dismisses the progress dialog, and thesetProgress()method sets the progress of the progress dialog.To use the
ProgressDialogHandlerclass, you would first create an instance of the class. Then, you would call theshow()method to show the progress dialog. Once the progress dialog is shown, you can call thesetProgress()method to update the progress of the progress dialog. Finally, you would call thedismiss()method to dismiss the progress dialog.Here is an example of how to use the
ProgressDialogHandlerclass:Code snippet
In this example, the progress dialog is shown and the progress is updated to 50%. Then, some work that takes some time is done. After the work is done, the progress is updated to 100% and the progress dialog is dismissed.