How to move again MainActivity after sending the m

2019-07-24 09:22发布

问题:

I am developing one app, in which I am sending the email with the help of intent. I am successfully sending the mail from MainActivity, but problem is that, as I am clicking on send button for sending the email, I am going to outside of the app. But after sending the email I want to come again on MainActivity. Here is my code.

 protected void sendEmail() {
    String[] TO = {"xxx@gmail.com"};
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
     emailIntent.setData(Uri.parse("mailto:"));
      emailIntent.setType("text/plain");

      emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "yyyy");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "zzzz.");

      try {
             startActivity(Intent.createChooser(emailIntent, "Send mail..."));
             finish();
          } catch (android.content.ActivityNotFoundException ex) {
             Toast.makeText(MainActivity.this, 
             "There is no email client installed.", Toast.LENGTH_SHORT).show();
          }

}

回答1:

You can remove finish() Or using startActivityForResult



回答2:

Instead of startActivity(Intent.createChooser(emailIntent, "Send mail..."));

You can use

startActivityForResult(Intent.createChooser(emailIntent, 1));

Your activity receives it in the onActivityResult() callback.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}