2

I try to add AlertDialog to do rename such as below code:

Button b = (Button)findViewById(R.id.btn);
b.setOnClickListener(
    new OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder renameDialog = new AlertDialog.Builder(AActivity.this);
            renameDialog.setTitle("Rename");
            final EditText newName = new EditText(AActivity.this);
            newName.setText(FilePath[i]);
            renameDialog.setView(newName);
            renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                }
            });

            renameDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                }
            });

            renameDialog.show();
        }
    }

    Intent it = new Intent(AActivity.this, BActivity.class);
    it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
    startActivity(it); 
    }
});

But the error occurs.

Activity AActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2b0278a8 that was originally added here

It was cause by new Intent start and pause the old one. But how to avoid it?

Flo
  • 27,355
  • 15
  • 87
  • 125
brian
  • 6,802
  • 29
  • 83
  • 124

4 Answers4

6

Launch the intent after you clicked a button on the dialog like this:

Button b = (Button)findViewById(R.id.btn);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    AlertDialog.Builder renameDialog = new AlertDialog.Builder(AActivity.this);
    renameDialog.setTitle("Rename");
    final EditText newName = new EditText(AActivity.this);
    newName.setText(FilePath[i]);
    renameDialog.setView(newName);

    renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            launchIntent();
        }
    });

    renameDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            launchIntent();
        }
    });

    renameDialog.show();
    }

    private void launchIntent() {
        Intent it = new Intent(AActivity.this, BActivity.class);
        it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
        startActivity(it); 
    }
});

I've put it in both buttons, but probably you only want it when the user clicks ok so it should only be placed there. YOu can do it without the function then.

J. Maes
  • 6,862
  • 5
  • 27
  • 33
3

please dismiss dialog when your activity is finish...

Rishi
  • 987
  • 6
  • 16
1

You should just set the Intent in the onClick() for positive.

If not,

Add in this line renameDialog.dismiss(); before your Intent

Hend
  • 589
  • 5
  • 15
  • 35
1
renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    Intent it = new Intent(AActivity.this, BActivity.class);
                    it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
                    startActivity(it); 
                }
            });

            renameDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
               //arg0 should change to other word like "dialog" (i think it more make sense) 
                 arg0.cancle();
                }
            });

            renameDialog.show();
        }
April Smith
  • 1,810
  • 2
  • 28
  • 52