tags:

views:

273

answers:

4

/*I have some wierd problem, inside OnMenuItemClickListener, i am calling an alert dialog which i made, but it seems like, when i call the alert dialog, it doesnt show it on right moment, only after onMenuItemClick finish. 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  A: 

Dialogs in Android aren't synchronous, so it's very likely that the UI thread is finishing onMenuItemClick() before creating/displaying your dialog (dialogs are managed by the enclosing Activity).

Edit: I misunderstood part of your question. Keep the call to alertDialogSaveFile() where it is and put the code that actually saves the file into the onClick() handler. Since dialogs in Android aren't synchronous, you need to perform the save action in the dialog callback itself. It's not possible to show a dialog, wait for the user to respond, and then get the result from the dialog.

Erich Douglass
A: 

Thank you! you think you could write some code sample? thanks again, ray.

rayman
Rather than adding a new answer, you can respond to someones post via the "comment" link right below it.
Mayra
You right, iam sorry.
rayman
A: 

I dont get it, i suppose to call the alertDialogSaveFile() method, from the inside of it? inside it's OK button handler?? hmm??

rayman
See my edit. Also, it's easier to see updates to an answer if you comment on an existing answer, rather than posting a new answer.
Erich Douglass
A: 

I get what you say, but it's pretty not logical to do, and not a "neat" coding, couldnt be other way, then push everything inside the okClick?

rayman