Reputation: 8408
The code below is what I am currently using for a list view item so that when I tap it, the email composer launches. However after sending a message it doesn't take me back to my app. How can I get this to return back to my app after sending an email? Also if anyone has a better way of doing this, please let me know.
All help is appreciated.
if(position == 7) {
Log.i("Send email", "");
String[] TO = {"[email protected]"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:[email protected]"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 1
Views: 1455
Reputation: 74
Your application exits because the finish() method you call in the try{... } will stop the activity immediately after the code before is executed. Remove or comment the finish() method to solve that.
Upvotes: 1
Reputation: 11191
You shouldn't finish your activity right after starting new activity from email intent. Please remove the finish() call and that should solve your problem. Once email is sent, the email activity will be destroyed and your previous activity should be displayed.
Upvotes: 2
Reputation: 1398
Your code doesn't use the javamail API but rather one of the email clients installed on the user's device. So this will cause the user to pick one sand end the mail specified in the EXTRA_TEXT part of the intent. From this part;
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
just remove the method 'finish()' from the code. This will cause the user to return to your application after tapping the back button when the mail is processed. The method finish() causes the activity to end calling the onStop() and even the onDetach() method if the user configures a certain option on his device for minimum power consumption.
Upvotes: 3