1

I have a very simple Thread.IUncaughtExceptionHandler as following

public class AirExceptionHandler : Java.Lang.Object, Thread.IUncaughtExceptionHandler
    {
        public static readonly string TAG = "AirExHandler";

        private MainActivity activity;
        private Thread.IUncaughtExceptionHandler defaultUEH;

        public AirExceptionHandler(MainActivity activity)
        {
            Log.Info(TAG, "Initialised");
            this.activity = activity;
            this.defaultUEH = Thread.DefaultUncaughtExceptionHandler;
        }

        public void SetActivity(MainActivity activity)
        {
            this.activity = activity;
        }
        public void UncaughtException(Thread t, Throwable e)
        {
            Log.Info(TAG, "Force Closed");
            JavaSystem.Exit(0);
        }
    }

In my Mainactivity, I am doing the following to initialise it in OnCreate

private static Handlers.AirExceptionHandler _unCaughtExceptionHandler;
.........
if (_unCaughtExceptionHandler == null)
   _unCaughtExceptionHandler = new Handlers.AirExceptionHandler(this);
else
   _unCaughtExceptionHandler.SetActivity(this);

if (Thread.DefaultUncaughtExceptionHandler != _unCaughtExceptionHandler)
    Thread.DefaultUncaughtExceptionHandler = (_unCaughtExceptionHandler);

I can see Log.Info(TAG, "Initialised"); triggered but when I force close my app, the UncaughtException does not get trigger

What am I doing wrong?

Ali
  • 2,702
  • 3
  • 32
  • 54
  • You could refer to the link below. It provide some suggestions about restart your application at the point the uncaughtException( ) method of the UncaughtExceptionHandler is called, the thread is dying.https://www.intertech.com/Blog/android-handling-the-unexpected/ – Wendy Zang - MSFT Feb 07 '20 at 02:22
  • thanks yes, Also tried that on the application level, same results, Force Stop is never reported in `UncaughtException` – Ali Feb 07 '20 at 05:39
  • As i know, the "Force close" will not appear when use uncaughtException. So avoid the "Force close", setup a restart mechanism when the crash happens anyway.https://stackoverflow.com/questions/2681499/android-how-to-auto-restart-application-after-its-been-force-closed – Wendy Zang - MSFT Feb 10 '20 at 09:05
  • Going through my research, I learnt that some devices such as (xamai) etc work in a way that if you swipe up to dismiss the app it is equal to.force close... I tried using Broadcast receivers, Job handlers, Worker process, non works if app is forced closed for me to trigger restart :( any idea what I can do – Ali Feb 10 '20 at 15:12
  • In Xamarin android, you could try to use Activity Lifecycle. OnStop and OnDestroy would be helpful. https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/activity-lifecycle/ In Xamarin.forms, you could try to use OnSleep. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/app-lifecycle – Wendy Zang - MSFT Feb 11 '20 at 08:08

0 Answers0