1

I am trying to send mail from my app.The problem is after successful/unsuccessful delivery of the email it doesn't return to the activity, meaning that onActivityResult() is not being called.

Here is my code:

String[] recipients = {"soham@gmail.com"};
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));

// prompts email clients only
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, recipients);

try{
    // the user can choose the email client
    startActivityForResult(Intent.createChooser(email, "Choose an email client from..."), 1);
}catch(android.content.ActivityNotFoundException ex){

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 1){
        if(resultCode == RESULT_OK){

        }else if (resultCode == RESULT_CANCELED){
        }
    }
}

I have checked this but not working for me. Can anyone tell me what am I doing wrong.

Edit

I got the problem.It will work fine in Activity.But it will not work on Fragment or FragmentActivity. All fragment is closing down forcefully.You can say my app is going on the background.How to solve this issue?Anybody got any idea.

Community
  • 1
  • 1
Soham
  • 4,397
  • 11
  • 43
  • 71
  • I just tested this and it gets into `onActivityResult()`, but it always gets into RESULT_CANCELED with no extras in the Intent. Take a look here: http://stackoverflow.com/questions/3778048/how-can-we-use-startactivityforresult-for-email-intent – Daniel Nugent Jun 30 '15 at 07:55
  • @DanielNugent, when I am starting the intent ,my app is closing down.It's not calling the onActivityResult() result,after finishing or cancelling.I am calling this from FragmentActivity,will be it a problem – Soham Jun 30 '15 at 08:02
  • Get look here : http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment – TooCool Jun 30 '15 at 10:01

1 Answers1

0

If i uderstand your problem correctly;

In your activity's onActivityResult part you can find your fragment

YourFragment fragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.your_framelayout_id);

And use your fragment's public method:

if(fragment != null)
{
   fragment.yourPublicMethod();   
}

In yourPublicMethod you can do whatever you want. I hope this helps you.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • thanks for your answer.My problem is if I call Intent.ACTION_SEND from a fragment or fragmentActivity then my app is closing down or going in the background. – Soham Jun 30 '15 at 10:50