I’m creating an application such that the user is in an Activity, and opens up an AlertDialog.
Inside the alertDialog is a button that opens a second activity, waits for the result and fills a textfield in the alertDialog.
Well, at least it is supposed to.
Heres a snippit to better illustrate the problem:
private String mFillAddrField = "";
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(data == null) {
...
} else {
Bundle b = data.getExtras(); //DATA retrieved from second activity successfully
mFillAddrField = b.getString("address");
}
}
}
...
public void displayAddScreen() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Create a new Grocery Store");
alert.setMessage("Create a new store by filling out the form below.");
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newstore_dialog, (ViewGroup) findViewById(R.id.nsd_root));
final EditText nameField = (EditText) layout.findViewById(R.id.nsd_et_name);
final EditText addrField = (EditText) layout.findViewById(R.id.nsd_et_address);
final Button addrSelect = (Button) layout.findViewById(R.id.nsd_b_address);
alert.setView(layout);
addrSelect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Open MAPS API return me an address
//Save address to text field
Intent i = new Intent(getApplicationContext(), GroceryMapActivity.class);
startActivityForResult(i,0);
addrField.setText(mFillAddrField); //OFFENDING CODE HERE
}
});
My problem is that the call to addrField.setText comes before the activity is completed (probably before its started too!), how can I wait here until the activity returns a result, then continue the rest of the code (setting the text)?
Move addrField.setText() to your onActivityResult() method
(You can’t stop and wait in the UI thread the way you seem to want to)
I’ve changed the supplied code to make addrField a class member and move setting it to match what you want to do: