jublikon jublikon - 10 days ago 5
Android Question

returnActivity events when calling startActivity method

I have an

Activity
and I start an appliction chooser with
startActivity
.

Question: How can I wait with the finishing the parent activity until the user has selected a preferred mail app?

Uri uri = Uri.parse("mailto:" + "someone@mail.com")
.buildUpon()
.appendQueryParameter("subject", "subject")
.appendQueryParameter("body", "body")
.build();

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(Intent.createChooser(emailIntent, "chooser Title"));

finish();

Answer

As stated in this post, you could use IntentPicker instead of IntentChooser

Intent intentPick = new Intent();
intentPick.setAction(Intent.ACTION_PICK_ACTIVITY);
intentPick.putExtra(Intent.EXTRA_TITLE, "Launch using");
intentPick.putExtra(Intent.EXTRA_INTENT, emailIntent);
this.startActivityForResult(intentPick, REQUEST_CODE_MY_PICK);
// You have just started a picker activity, 

Then you can listen for the result of the intent pick by adding the following callback method in your activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE_MY_PICK) {
         // start the activity the user picked from the list
         startActivity(data);

         //you can finish() your activity here
    }
}
Comments