-2

I'm creating an application that sends many requests to server from several Activity a fragment. i want to show ProgressDialog in request send methods, in this case, I want to write one time a code to show ProgressDialog and I don't want to write show progress dialog for every request.

this is my DataAccess Class for Connect to the server and send a request that every request use this class

public class DataAccess {

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {

        client.get(getAbsoluteUrl(url), params, responseHandler);

    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static  void  start() {
    }

    private static String getAbsoluteUrl(String relativeUrl) {

        return Settings.serverLink + relativeUrl;
    }
}

Can I show ProgressDialog in this Class?

1 Answers1

0

You can create a separate class for ProgressDialogUtil with create/Update/cancel method and call the method from whichever activity you want.

Call Show method for every Request and Dismiss when you got the response.

Create this class and call methods ->

public class MyProgressDialog {

    private static ProgressDialog progressDialog;

    public static void show(Context context, int messageResourceId) {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }

        int style;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            style = android.R.style.Theme_Material_Light_Dialog;
        } else {
            //noinspection deprecation
            style = ProgressDialog.THEME_HOLO_LIGHT;
        }

        progressDialog = new ProgressDialog(context, style);
        progressDialog.setMessage(context.getResources().getString(messageResourceId));
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    public static void dismiss() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

}
Jay Dangar
  • 3,271
  • 1
  • 16
  • 35