Alert dialog opens

/ * I have a problem with wierd, inside OnMenuItemClickListener I call the warning dialog that I did, but it seems that when I call the warning dialog, it does not appear at the right moment, only after onMenuItemClick finishes. what am i doing worng? * /

class MyListMenuListener implements OnMenuItemClickListener
    {

        private String TAG;

        @Override
        public boolean onMenuItemClick(MenuItem item)
        {
            if (item.getItemId() == saveRoute.getItemId())
            {                   
                alertDialogSaveFile();
                //nameInput = "testone.txt";
                //some operations
//                                      ...
 //                                      return true;
            }

// ...

/*the wierd thing is that the alert dialog doesnt show up on the same moment i call it..
only after the onMenuItemClick operation ends (with return)
and this is how my alertdialog looks like:*/

        private void alertDialogSaveFile()
{

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Save your current map");
        alert.setMessage("Please insert name for this route");
        final EditText saveNameInput = new EditText(TwittListActivity.this);

        alert.setView(saveNameInput);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                nameInput = saveNameInput.getText().toString();
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
            }
        });
         AlertDialog ad = alert.create();
         ad.show();
    }


//Thanks!
//ray.
+3
source share
1 answer

Dialogs in Android are not synchronous, so it is very likely that the user interface thread completes onMenuItemClick()before creating / displaying your dialog box (dialog boxes are controlled by activating Activity).

. . alertDialogSaveFile(), , , onClick(). Android , . , , .

+3

Source: https://habr.com/ru/post/1730213/


All Articles