---
title: "Maintain progress bar state using Volley Android"  
description: "Maintain progress bar state using Volley Android"  
author: "Anonymous User"  
published: 2016-01-12  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/33863/maintain-progress-bar-state-using-volley-android  
category: "android"  
tags: ["android", "android libraries"]  
reading_time: 4 minutes  

---

# Maintain progress bar state using Volley Android

It is easy to maintain [progress bar](https://www.mindstick.com/articles/12747/android-progress-bar) state when i use AysncTask with fragments [Callback](https://www.mindstick.com/articles/152/using-the-callback) but how should i achieve it with volley? I can't use [AsyncTask](https://www.mindstick.com/forum/158886/what-is-the-purpose-of-asynctask-in-android-and-when-should-it-be-used) because it is outdated and volley is better and faster. Any Help or Hint will be grateful.\
I am using [google](https://www.mindstick.com/articles/43833/google-lighthouse-and-how-is-it-changing-the-way-we-development-and-design-websites)'s volley to [Post](https://www.mindstick.com/articles/12829/aspects-that-make-australia-post-shipping-extension-a-useful-tool) and Get Requests

## Replies

### Reply by Aryan Kumar

Sure, here is how to maintain the progress bar state using Volley Android:

Code snippet

```plaintext
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

```plaintext
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.

### Reply by Anonymous User

\
I think there are misconceptions here.\
First off, Volley faster than AsyncTask.\
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](https://yourviews.mindstick.com/view/284/devotees-arrived-in-prayagraj-while-work-is-still-in-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.


---

Original Source: https://www.mindstick.com/forum/33863/maintain-progress-bar-state-using-volley-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
