0

I am trying to show a custom dialog within Activity and this error occur. I know why this error occur from here and here and also know it's solution. But the problem is I can't find the problem in my code. I've spent anlot of time in finding the error with no luck. I've called below method on button click

public void changePassword(View view){
    DialogChangePassword dialogChangePassword = new DialogChangePassword(ActivityMyAccount.this);
    dialogChangePassword.show();
    Window window = dialogChangePassword.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}

I am showing the dialog here while Activity should live. My question;

  1. I can't dismiss the dialog here, I am dismissing it from within Dialog class.
  2. I am not finishing the activity. If error occurs because the activity is finishing, why is it finishing?
  3. If error is not because of finishing the activity then why does this error occur?
  4. I have a NetworkOperation to be performed in Dialog class, does it have to do something with that?

Here is my Dialog class

public class DialogChangePassword extends Dialog {
    Context context;
    Button confirm, cancel;

    CustomEditText edtOldPwd, edtNewPwd, edtConfirmNewPwd;

    ProgressDialog progressDialog;

    String PASSWORD_INCORRECT_MESSAGE = "Old password is incorrect.";

    public DialogChangePassword(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_change_password);

        if(progressDialog != null)
            progressDialog = null;

        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("Please wait...");
        progressDialog.setMessage("Updating Password.");
        progressDialog.show();


        confirm = (Button) findViewById(R.id.btnChangePassword);
        cancel = (Button) findViewById(R.id.btnCancel);

        edtOldPwd = (CustomEditText) findViewById(R.id.edtOldPassword);
        edtNewPwd = (CustomEditText) findViewById(R.id.edtNewPassword);
        edtConfirmNewPwd = (CustomEditText) findViewById(R.id.edtConfirmNewPassword);

        edtConfirmNewPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!edtNewPwd.getText().toString().equals(edtConfirmNewPwd.getText().toString()))
                    edtConfirmNewPwd.setError("Password doesn't match");
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!edtNewPwd.getText().toString().equals(edtConfirmNewPwd.getText().toString())) {
                    Toast.makeText(context, "Password doesn't match",         Toast.LENGTH_SHORT).show();
                    progressDialog.cancel();
                    return;
                }
                HashMap<String, String> params = new HashMap<>();
                params.put("email", Utility.classUser.getEmailId());
                params.put("password", edtOldPwd.getText().toString());
                params.put("newPassword", edtNewPwd.getText().toString());
                new NetworkOperation().execute(params);
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private class NetworkOperation extends AsyncTask<HashMap<String,String>, Void, String> {
        @Override
        protected String doInBackground(HashMap<String, String>... params) {
            try {
                ClassAPIRoutes routes = ClassAPIRoutes.initializeRoutes();
                return routes.editUserPassword(params[0]);
            } catch (final IOException e) {
                e.printStackTrace();
                getOwnerActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
                return "error";
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressDialog.cancel();
            dismiss();
            if(!s.equals("error")){
                JSONObject jsonObject;
                try {
                    jsonObject = new JSONObject(s);
                    if (jsonObject.get("success").toString().equals("true")) {
                        Toast.makeText(context, "Password successfully changed.", Toast.LENGTH_SHORT).show();
                        dismiss();
                    } else if (jsonObject.get("success").toString().equals("false")) {
                        if (jsonObject.get("message").toString().equals(PASSWORD_INCORRECT_MESSAGE)) {
                            Toast.makeText(context, PASSWORD_INCORRECT_MESSAGE, Toast.LENGTH_SHORT).show();
                            resetFields();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), s, Toast.LENGTH_SHORT).show();
                }
            }

        }
    }

    private void resetFields(){
        edtOldPwd.setText("");
        edtNewPwd.setText("");
        edtConfirmNewPwd.setText("");
    }

}
Community
  • 1
  • 1
Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
  • dismiss your dialogs/ progress dialogs/ alert dialogs before finishing your activity and you will have no leaks :) – SripadRaj Jul 27 '16 at 10:10
  • Your dialog holds to the context of your activity. As @SripadRaj said, dismiss your dialog before finishing your activity, maybe put it in onStop. – gitter Jul 27 '16 at 10:20
  • @gitter I want to return to the activity again after changing the password. Why the activity is finishing? If it does, `DialogChangePassword` would be dismissed and I won't be able to change password. Well, I tried with no luck – Waqas Ahmed Ansari Jul 27 '16 at 10:34
  • Hmm. Having an AsyncTask in a dialog looks very error prone, given that AsyncTask in general is infamous for memory leaks. Try moving your dismiss call to the end of onPostExecute once? – gitter Jul 27 '16 at 10:42
  • I am already calling `dismiss()` inside `onPostExecute()` – Waqas Ahmed Ansari Jul 27 '16 at 10:44
  • it will be better that you have object of dialog class in activity/fragment and from there you call the dismiss method. You may be dismising the wrote dialog object and that's the reason you are seeing this issue. Could help you more if you provide more source code here. – Sandeep Agrawal Jul 27 '16 at 11:54

1 Answers1

0

I've removed showing progress dialog from onCreate(...) and put it in Async class inside OnPreExecute(...) and it's working fine

Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30