I need to be able to tell when a spawned activity (via an intent) has completed, how would I do so?
This is what I have:
alertDialog.setButton2("Text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String uri = "smsto:" + "";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
intent.putExtra("sms_body", PASSWORD_GENERATOR
.generatePasswordForSeed(seedText, hourToUse));
intent.putExtra("compose_mode", true);
// -- open the text message activity
startActivity(intent);
// -- I need to reset the calling activity now, but AFTER the text message activity has completed. Right now the SMS closes right away as I have no wait in...
finish();
startActivity(getIntent());
}
});
EDIT #1
Per the suggestions below, I’ve made some modifications. Now, however, the launched SMS activity just “sits there” once the text is sent. I can’t figure out how to get it to return to the calling activity. This is what I have:
alertDialog.setButton2("Text", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String uri = "smsto:" + "";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
intent.putExtra("sms_body", PASSWORD_GENERATOR
.generatePasswordForSeed(seedText, hourToUse));
intent.putExtra("compose_mode", true);
startActivityForResult(intent, Activity.RESULT_OK);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
startActivity(getIntent());
}
}, new IntentFilter("SMS_SENT"));
ContentResolver contentResolver = getContentResolver();
Handler handler = new Handler();
contentResolver.registerContentObserver(Uri
.parse("content://sms"), true, new ContentObserver(
handler) {
@Override
public boolean deliverSelfNotifications() {
setResult(Activity.RESULT_OK);
finish();
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
setResult(Activity.RESULT_OK);
finish();
}
});
}
});
alertDialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
finish();
startActivity(getIntent());
}
Followup Answer for second question:
Out of curiosity, have you considered sending the text without creating an activity? The reason I ask is because it seems, from what I can tell, no user experience is happening within your SMS activity. If you don’t have views and user interactions then maybe just defining a thread or creating a helper class would do the job.
Original Answer for first question:
Try using startActivityForResult(Intent, int). Your current activity can get notified when the text message activity finishes.