1

For my Monodroid application, I'd like to do the following after an unhandled exception:

  1. Send the error to the server.
  2. Notify the user that the application has crashed (perhaps with a toast message).
  3. Exit the application gracefully.

I've implemented #1, but I'm struggling to implement #2 and #3.

Toast doesn't seem to be available after an unhandled exception, and I've been warned that it's a bad idea to exit an app on a user's behalf.

Can anyone point me in the right direction?

Here is my code:

using System;
using Android.App;
using Android.Runtime;
using Android.Widget;

namespace MyAppsNamespace
{
    [Application]
    public class MyApplication : Application
    {
        public static MyApplication Current { get; private set; }

        public MyApplication (IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer) : base(handle, transfer)
        {
            Current = this;
        }

        public override void OnCreate()
        {
            base.OnCreate();
            AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) => LogException(args.Exception);
        }

        public static void LogException(Exception exception)
        {
            var phoneId = Guid.NewGuid(); // just for testing purposes
            var client = AppConfig.ErrorLoggingServiceClient;
            var response = client.Send<ErrorLoggingResponse>(new ErrorLoggingEntry
                {
                    PhoneId = phoneId,
                    ErrorTime = DateTime.UtcNow,
                    Message = exception.Message,
                    StackTrace = exception.StackTrace
                }); // This works fine (i.e. I've implemented #1)
            Toast.MakeText(Context, String.Format("An error occurred. Please call Prod Support at 1-800-555-1212. [Phone Id: '{0}']", phoneId), ToastLength.Short).Show(); // This has no impact.
        }
    }
}
Community
  • 1
  • 1
Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • 1
    As to #2, as far as can tell you intercept exception too late - your activity is already stopped or otherwise brought to the state where its ability to do something with the UI is no longer available. You may want to try to start another activity instead of showing a Toast directly - this another activity may do nothing but show the Toast you want. – amoiseyev Jul 11 '13 at 17:59
  • 3
    As to #3, I'm not even sure what do you mean by "exit gracefully". App has already crashed, and you get control at the very end, right before exit - if there was something to do, like save unsaved data etc., it is too late to do. And I believe that you can trust android itself to terminate crashing app in the safest way possible - so I'd say "do not do anything". – amoiseyev Jul 11 '13 at 18:03

1 Answers1

2

Not sure what context Context refers to in:

Toast.MakeText(Context, "ERROR", ToastLength.Short).Show();

You could try

Toast.MakeText(YourMainApplication.this, "ERROR", ToastLength.Short).Show();

instead.

petter
  • 1,765
  • 18
  • 22