I have a dialog with a list of buttons. When I press a button another dialog is created with an EditText and a button to access the phone's contacts list and select a contact to send an SMS.
The body of the SMS will depend on the pressed button from the first dialog. The problem is that when I return from the Contacts Activity, I don't know which button was pressed and because of that I can't build the string which will be the SMS body.
I tried this:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.putExtra("str", string); // 'string' will be the SMS body
startActivityForResult(intent, 1);
.
.
.
.
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
.
.
.
String str = data.getStringExtra("str"); // returns NULL
.
.
.
}
break;
}
}
But this returns NULL.
How can I do this ?
EDIT - This is how I create the 2nd dialog
String str = ".........";
AlertDialog.Builder builder = new AlertDialog.Builder(THIS_ACTIVITY.this);
builder.setTitle("Send SMS to...");
LinearLayout layout = new LinearLayout(builder.getContext());
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout layout_contact = new LinearLayout(builder.getContext());
layout_contact.setOrientation(LinearLayout.HORIZONTAL);
TextView text = new TextView(builder.getContext());
text.setText("SMS destination:");
EditText phone = new EditText(builder.getContext());
Button btn = new Button(builder.getContext());
btn.setText("Send");
Button btn_contacts = new Button(builder.getContext());
btn_contacts.setText("Search contact");
layout.addView(text);
layout.addView(phone);
layout_contact.addView(btn_contacts);
layout_contact.addView(btn);
layout.addView(layout_contact);
builder.setView(layout);
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Dialog d = builder.create();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String destination_phone = phone.getText().toString();
try{
sendSMS(destination_phone, str);
}catch (Exception ex) {
Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
});
d.show();
btn_contacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.putExtra("str", str);
startActivityForResult(intent, 1);
}
});