2

I would like to have a SMS intent be populated for the user, then launch the intent and after the SMS is sent, return to my app to continue to execute code. So thus far I use this to setup the Intent:

 Uri uri = Uri.parse("smsto:" + cellNumberEditText.getText().toString());
 Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
 smsIntent.putExtra("sms_body", smsMessageEditText.getText().toString());
 smsIntent.putExtra("exit_on_sent", true);
 startActivityForResult(smsIntent,1);

Note I add the Extra exit_on_sent parameter to the intent.

Then off course I catch the onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1)
    {
        //Do some post sending work
    }
}

The problem is, after debugging, I noticed that as soon as the intent launches, the onActivityResult fires immediately, not after the the intent closes. Furthermore, the SMS app does not close after sending even though the parameter is set. I even added android:launchMode="singleInstance" to my Manifest.xml. What am I missing?

Marnus Steyn
  • 1,053
  • 2
  • 16
  • 44

1 Answers1

-1

You should add setResult(Activity.RESULT_OK, data); and finish(); in the requested activity. The latest return back to the requester activity.

Guille
  • 370
  • 2
  • 7
  • 21
  • How can I do that? The SMS intent I launch is closed off from my side, i cannot change the code it uses? If I had launched an activity I created myself that would have been possible – Marnus Steyn Jun 02 '17 at 11:38
  • i've found this way: `String number = "12346556"; // The number on which you want to send SMS startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null))); ` – Guille Jun 02 '17 at 17:21