Domanda

I'm using a custom dialog in onpostexecute method in AsyncTask, it is being popped twice. When the user clicks on a button the dialog has to be closed, this seems to work fine.

Can someone shed some light on why it is being called twice ?

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    if (pDialog != null) {
        pDialog.dismiss();
    }

    try {
        if (responseFromServer.contains("x")) {
            // Pop up to create password
            final Dialog dialog = new Dialog(getActivity());
            dialog.setContentView(R.layout.dialog_password);
            dialog.setTitle("Title...");
            dialog.setCancelable(false);

            final TextView etpassword = (TextView) dialog.findViewById(R.id.etpassword_dialog);
            final Button btnpassword = (Button) dialog
                    .findViewById(R.id.btnsavepassword_dialog);

            btnpassword.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (etpassword.getText().toString().length() == 0) {
                        Toast.makeText(getActivity(), "Enter password", Toast.LENGTH_SHORT)
                                .show();
                    } else if (etpassword.getText().toString().length() < 6) {
                        Toast.makeText(getActivity(),
                                "Password should contain minimmum 6 characters",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        dialog.dismiss();

                    }

                }
            });

            if (!dialog.isShowing()) {
                dialog.show();
            }

        }

        else {
            Toast.makeText(getActivity(), "Unexpected error occurred. Please try again",
                    Toast.LENGTH_SHORT).show();
        }

    } catch (Exception e) {
        Log.v("Main FRagment FB async::::::", e.getMessage());
    }

}    
È stato utile?

Soluzione

You can write following condition before display a custom dialog,

if ( !dialog.isShowing() )
{
   dialog.show();
}

Altri suggerimenti

your code is correct there is no problem in code. Check button click. i think you are calling twice execute() method of AsyncTask. Can you post the calling code like how are you calling AsyncTask fron button click.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top