Deepak
Deepak

Reputation: 192

How to return to activity after sending a mail in android?

All working fine and the mail is also properly sending .But I cant return to the activity after sending mail.

Current screen history is false.I have used start Activity with result code also.But cant make it.

Could some one guide or provide some sample code it will be really usefull for me

bookByMail.setOnClickListener(new View.OnClickListener() {// sending mail details
                @Override
                public void onClick(View v) {                   
                    String mailId ="[email protected]";
                      String sms = messageSummary.getText().toString();
                      String subject="Bottle Order";
                      Intent email = new Intent();
                      email.putExtra(Intent.EXTRA_EMAIL,new String[]{"[email protected]","[email protected]"});
                      email.putExtra(Intent.EXTRA_CC, new String[]{"[email protected]"});
                      email.putExtra(Intent.EXTRA_BCC, new String[]{"[email protected]"});
                      email.putExtra(Intent.EXTRA_SUBJECT, subject);
                      email.putExtra(Intent.EXTRA_TEXT, sms);           
                      email.setType("sms/rfc822");   
                      //email.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);                
                     startActivity(Intent.createChooser(email, "Choose an Email client :"));                                      
                                }                                           
            });         
    dialog.show();
    }   
        });
        }

Upvotes: 2

Views: 4887

Answers (4)

Kushal
Kushal

Reputation: 8498

In this situation, you should use startActivityForResult() method.

This method starts a new activity and waits for the result code which comes from new activity when new activity exits

You need to do some changes like :

bookByMail.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {                   
        String mailId ="[email protected]";
        String sms = messageSummary.getText().toString();
        String subject="Bottle Order";
        Intent email = new Intent();
        email.putExtra(Intent.EXTRA_EMAIL,new String[]{"[email protected]","[email protected]"});
        email.putExtra(Intent.EXTRA_CC, new String[]{"[email protected]"});
        email.putExtra(Intent.EXTRA_BCC, new String[]{"[email protected]"});
        email.putExtra(Intent.EXTRA_SUBJECT, subject);
        email.putExtra(Intent.EXTRA_TEXT, sms);           
        email.setType("sms/rfc822");   
        startActivityForResult(Intent.createChooser(email, "Choose an Email client :"), 800);                                      
    }                                           
});         

And wait for response by overriding onActivityResult() method :

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 800) {
        //Called when returning from your email intent
    }
}

Upvotes: 2

Samir
Samir

Reputation: 6605

Inside Activity onCreate Method

    btn_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String isim = innerName.getText().toString();
            String yurtIsmi = innerYurtName.getText().toString();
            String telNo = innerTelNo.getText().toString();
            String message = " ";
            String messageTitle = isim + " Hello ";
            String messageBody = isim + " " + yurtIsmi + "\n" + " " + ulke_sehir + "\n" + " " + telNo + "\n" + " " + message;
            try {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                i.putExtra(Intent.EXTRA_SUBJECT, messageTitle);
                i.putExtra(Intent.EXTRA_TEXT, messageBody);
                // startActivity(Intent.createChooser(i, "Send mail..."));
                startActivityForResult(Intent.createChooser(i, "Choose an Email client :"),0);
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(KayitAct.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
        }
    });

receive the result inside the same activity

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    String sam = "";
    if (requestCode  == 0) { // Activity.RESULT_OK
        Toast.makeText(KayitAct.this, "Kayıt Talebiniz Tarfımıza İletildi \nEn Kısa Sürede Dönüş Yapılacaktır", Toast.LENGTH_LONG).show();
        Intent intentGoToMenu = new Intent(KayitAct.this, LoginActivity.class);
        startActivity(intentGoToMenu);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        finish();
    }

}

Upvotes: 1

stevehs17
stevehs17

Reputation: 1476

In my testing, the code below launches an email client and starts composing an email message. When I click Send or Discard, I am automatically returned to the activity that executed the code to launch the email client. Note that the code uses startActivity() and not startActivityForResult().

What does not work using this code is returning to the activity that launched the email client using Up navigation (clicking the arrow on the left side of the action bar). And using startActivityForResult() instead of startActivity() made no difference.

public void onClickSendEmail(View unused) {
    String body = "App Version Code: " + BuildConfig.VERSION_CODE;
    body += "\nApp Version Name: " + BuildConfig.VERSION_NAME;
    body += "\nOS API Level: " + android.os.Build.VERSION.SDK_INT;
    body += "\nDevice: " + android.os.Build.DEVICE;
    body += "\nModel: " + android.os.Build.MODEL;
    body += "\nProduct: " + android.os.Build.PRODUCT;

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("plain/text");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { getString(R.string.roamfree_email_addess) });
    String userId = "fakeid";
    intent.putExtra(Intent.EXTRA_SUBJECT, "RoamFree Feedback: User Id " + userId);
    intent.putExtra(Intent.EXTRA_TEXT, body);
    startActivity(Intent.createChooser(intent, "Choose Email Client:"));
}

Upvotes: 1

Yash Sampat
Yash Sampat

Reputation: 30611

Use startActivityForResult() instead of startActivity(). This will return to the starting Activity after the Intent action is completed. See the Getting a Result from an Activity post for an example.

Upvotes: 2

Related Questions