Link to home
Start Free TrialLog in
Avatar of spazjr01
spazjr01Flag for United States of America

asked on

How do I stop my application from crashing when starting an android email intent? I had a Xamarin Crossplatform netstandard 1.6 project for Android and iOS.

How do I stop my application from crashing when starting an android email intent?  I had a Xamarin Crossplatform netstandard 1.6 project for Android and iOS.  
 
I developed a dependency service for sending an email from xamarin.forms. This used to work just fine. But, after a few Xamarin.Forms updates and related Xamarin.Android... API updates, this stopped working. At one point, I had to change the way I get the context to call the StartActivity(email) method.

Now, the email activity starts and I am able to send the email, but the app that called it crashes. And, I cannot figure out why. Is there something that I am doing wrong?


Method in common project (NetStandard 1.6)

private async Task EmailFileAsync(EmailContent emailContent)
 {
 var emailer = DependencyService.Get();
 Tuple<bool, string> response = new Tuple<bool, string>(false, "Unable to get an Email Manager from the device. ");
        if (emailer != null)
        {
            response = await emailer.SendEmailAsync(emailContent);
        }

        if (!response.Item1)
        {
            await DisplayAlert("Send TRN Failure", response.Item2, "OK");
        }

        return response.Item1;

}

Dependency Service in Android project

using Android.App;
 using Android.Content;
 using System;
 using System.IO;
 using System.Threading.Tasks;
 using TPS_Mobile_NS.Droid.InterfacesImplemented;
 using TPS_Mobile_NS.Interfaces;
 using TPS_Mobile_NS.Models;
 using Xamarin.Forms;

[assembly: Dependency(typeof(EmailManager))]
 namespace TPS_Mobile_NS.Droid.InterfacesImplemented
 {
 public class EmailManager : Activity, IEmailManager
 {
 public Task<Tuple<bool, string>> SendEmailAsync(EmailContent emailContent)
 {
 var sendWasSuccessful = false;
 var errorMessage = string.Empty;
 try
 {
 var email = new Intent(Intent.ActionSend);
            // Finish email.
            email.PutExtra(Intent.ExtraSubject, emailContent.Subject);
            email.PutExtra(Intent.ExtraText, emailContent.Body);        
            email.SetType("message/rfc822");
            email.AddFlags(ActivityFlags.NewTask);

            Context context = Android.App.Application.Context;
            context.StartActivity(email);

            sendWasSuccessful = true;
            return Task.FromResult(new Tuple<bool, string>(sendWasSuccessful, errorMessage));
        }
        catch(Exception ex)
        {
            errorMessage = ex.Message;
            return Task.FromResult(new Tuple<bool, string>(sendWasSuccessful, errorMessage));
        }

    }
Avatar of spazjr01
spazjr01
Flag of United States of America image

ASKER

I have conducted more testing and have determine (as far as I can tell) that the problem is not specific to the "email" intent.  I changed the intent to open a browser and produced the same result.  That is, the browser opens, but the app that is starting the new activity is crashing(i.e. not gracefully fading into the activity stack "background" as it used to).  All of this trouble started with the upgrade to Xamarin.Forms 2.5, I think.  

Maybe this additional information will help someone think of a solution:
// intent to start browser
string url = "http://www.google.com";
Intent browser = new Intent(Intent.ActionView, Android.Net.Uri.Parse(url));
Context context = Android.App.Application.Context;
context.StartActivity(browser);
I discovered further research that none of the posted code was the problem.  The problem was caused earlier in the code from a poorly developed extended class.   This class was used to create and store a file.  In the process of doing that a null reference exception was created.  However, it wasn't showing up as an error until the call to start the new Activity was made.  So, it appeared as thought the StartActivity action was the cause of the problem.  Once the class logic was fixed, there was no problem with the StartActivity call.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.