akhil Rao
akhil Rao

Reputation: 1169

Progressdialog is not getting dismissed inside the volley response block

My app performs uploading operation to backend php server.My problem is i cannot dismiss the progressdialog inside volley Onresponse() method.Onresponse() method gets executed and toast is displayed in background of the Progressdialog so that means Onresponse() gets called currectly.

I declared ProgressDialog variable in public

onCreate() method look like this

    ProgressDialog dialog;
    protected void onCreate(Bundle savedInstanceState) 
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.signup);

            btn.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                      dialog=new ProgressDialog(context,ProgressDialog.STYLE_SPINNER);
                      dialog.setIndeterminate(true);
                      dialog.show(context,"Signing up","Please wait...");
                      login_call();
             }

Login_call() performs volley request and inside the onResponse() i need to dismiss the dialog but its not working.

    public void login_call() 
    {
         RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
         StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.56.1/sign.php", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if (response.equals("success")) 
            {

                dialog.dismiss();

                getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            } 
            else 
            {
                dialog.dismiss()
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            dialog.dismiss();
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }


    ) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> param = new HashMap<>();
            param.put("user",user.getText().toString());
            param.put("pass",pass.getText().toString());
            param.put("name",name.getText().toString());
            param.put("mob",mob.getText().toString());
            param.put("email",email.getText().toString());
           param.put("photo",photo);
            return param;
        }
    };

    queue.add(request);
 }

Toast message is displaying correctly but progressdialog is not gettting dismissed.

enter image description here

Any ideas to solve the problem??

Upvotes: 1

Views: 1565

Answers (3)

Janki Gadhiya
Janki Gadhiya

Reputation: 4510

Just change this :

RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.56.1/sign.php", new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

        dialog.dismiss(); // write it there

        if (response.equals("success")) {

       // dialog.dismiss(); remove this from here..!!


            getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

            );
        } else {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       dialog.dismiss();
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
    }
}

Edit : set this

dialog.setIndeterminate(false);

Upvotes: 1

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

Change your Dialog.show method to use this version: show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable) like this:

final ProgressDialog dialog = ProgressDialog.show(context, "Signing up","Please wait...", false, true); 

So your onClick code should change to something like this:

public void onClick(View view) {
    final ProgressDialog dialog = ProgressDialog.show(context, "Signing up","Please wait...", false, true); 
    login_call();
     }

Please give this a try and let me know if it helps.

Upvotes: 1

Ivan
Ivan

Reputation: 978

first in first you didn't check all conditions

new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        if (response.equals("success")) {
         dialog.dismiss();
            getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        **} else {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
        }**
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       dialog.dismiss();
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
    }

The else block doesn't hide the dialog so you should change it in this way

new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        dialog.dismiss();
        if (response.equals("success")) {
            getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        } else {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
       dialog.dismiss();
        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
    }

I think that the mistake is this, of course assuming that your dialog is accessible from the callback

Upvotes: 1

Related Questions