How to move again MainActivity after sending the mail?

佐手、 提交于 2019-12-24 01:27:43

问题


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)
        }
    }
}


来源:https://stackoverflow.com/questions/21673371/how-to-move-again-mainactivity-after-sending-the-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!