简体   繁体   中英

Default mail client continues to be running in background after mail is sent

I am using ACTION_SEND to send mails from my application. It works fine but the problem is, since am using Intent.FLAG_ACTIVITY_NEW_TASK flag to send the mail,the eMail client continues to run in the background even after the mail is sent. Worst it still shows my email as draft (yet to be sent).

I use the below code to send mail from a non-activity

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("message/rfc822");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Report issue: \""+mIssueTitle+"\"");
    intent.putExtra(Intent.EXTRA_TEXT, mailBody);       
    String[] mailIds = new String[] {getReportingMailId()};
    intent.putExtra(Intent.EXTRA_EMAIL, mailIds);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    App.getContext().startActivity( intent );

How can i trigger the mail client to send the mail from a non activity without the mail client from running in background forever

Ok finally figured out the solution.

The problem is, am using Application#Context to start the activity. When ever we use Application#Context a new Task is created ( that's why, adding the flag Intent.FLAG_ACTIVITY_NEW_TASK in the intent if we don't add this Flag then app crashes) .

To avoid creation of new task all we need to do is use the right context ie., use the activities context. ("Be careful and avoid memory leaks in case storing the activity context reference"). So the code is as below

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_SUBJECT, "Report issue: \""+mIssueTitle+"\"");
intent.putExtra(Intent.EXTRA_TEXT, mailBody);       
String[] mailIds = new String[] {getReportingMailId()};
intent.putExtra(Intent.EXTRA_EMAIL, mailIds);

enclosingActivityContext.startActivity( intent );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM