0

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);
}
});
  • Take a look at this answer: https://stackoverflow.com/a/33431042/1806193 Maybe it will help you – luizfzs Jun 26 '17 at 17:08
  • Hi, what I've interpreted from your question is that you want to keep the button highlighted so that user will get to know that this specific button was selected, right? In that case, why don't you simply change the `background` of a button, and in the **dialog's** `onClick()` just don't `dismiss` the dialog, so that it will still be there? – Daksh Gargas Jun 26 '17 at 17:11

1 Answers1

0

When the user press the button in your activity, you need to call setResult providing a bundle before finishing your activity.

An example:

Intent resultIntent = new Intent();
resultIntent.putExtra("str", "String"); 
setResult(Activity.RESULT_OK, resultIntent);
finish();

--EDIT-- Now I understood your question, what you want is to call the select contacts activity and when you receive your the callback, you have the info.

Your solution is to just create a field inside your activity. The activity that will be called, is the someone that called. What you need to ensure is that you persist this field if the activity is killed due to low memory. To do it, just implement the onSaveInstanceBundle and get the information back in the activity`s onCreate.

jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • but how do I use that ? the "result" I want to change is the result from the contacts list activity but if somehow I change that, I lose the contact chosen by the user – João Amorim Jun 26 '17 at 18:33