Daniel Conde Marin
Daniel Conde Marin

Reputation: 7742

Android asynchronous dialogs causing problems?

I'm quite aware that Android manages its Dialogs in an asynchronous way for improving performance,memory improvement,etc. But the problem is that I have encountered a simple situation where I need to fetch an input given by the user in order to follow with the execution of the app.Since I am using a Dialog this all messes up cause the flow of the app continues wether or not the user has written something in my prompt dialog. Is there any way of solving this????

Here is the code:

@Override 
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case 1:
        startActivity(new Intent("HOMESCREEN"));
        break;
    case 2:
        String searched = prompt("Search", HomeScreen.this);
        Intent data = new Intent("RESULTSSCREEN");
        data.putExtra("SearchCriteria", "");
        startActivity(data);
        break;
    case 3:
        //....
        break;
    }

    return true;
}

Here is the code for prompt:

public String prompt(String message, Context ctx)
    {
        //load some kind of a view
        LayoutInflater li = LayoutInflater.from(ctx);
        View view = li.inflate(R.layout.prompt_layout, null);
        //get a builder and set the view
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle("Search");
        builder.setView(view);
        //add buttons and listener
        PromptListener pl = new PromptListener(view);
        builder.setPositiveButton("OK", pl);
        builder.setNegativeButton("Cancel", pl);
        //get the dialog
        AlertDialog ad = builder.create();
        //show
        ad.show();

        return pl.getPromptReply();
    }

The problem is that the intent data is executing before the user finishes up writing the search criteria. Regards

Upvotes: 0

Views: 147

Answers (3)

Andrii Chernenko
Andrii Chernenko

Reputation: 10204

You can stick to your AlertDialog, but you will need to set your positive and negative buttons in a different way (check this for example):

final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText();
      // Do something with value!
    }
});

Upvotes: 0

newbyca
newbyca

Reputation: 1513

Instead of creating an instance of PromptListener inside Prompt and having it return a string, you should be passing an instance of PromptListener into Prompt and define a method inside PromptListener that can pass along the new text value. Something like this:

@Override 
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case 1:
        startActivity(new Intent("HOMESCREEN"));
        break;
    case 2:
        prompt("Search", HomeScreen.this, new PromptListener(){
            @Override
            public void TheTextIsReady(String text){
                Intent data = new Intent("RESULTSSCREEN");
                data.putExtra("SearchCriteria", text);
                startActivity(data);
            }
        });
        break;
    case 3:
        //....
        break;
    }

    return true;
}

*Note, you didn't post the code for PromptListener, so in this case 'TheTextIsReady(...)' is just something I made up.

Upvotes: 0

Jameo
Jameo

Reputation: 4607

Don't use an Android Dialog. Instead create a new activity that acts like a dialog with this in the manifest

android:name="com.namespace.DialogActivity"
android:theme="@android:style/Theme.Dialog"

Then, you will use startActivityForResult(intent) from your main activity. You can capture the result with onActivityResult

I've had the same problems trying to do this, trust me this is what you are looking for. It gives you much more flexibility for customizing the dialog, and allows for capturing results

Upvotes: 1

Related Questions