Hallupa
Hallupa

Reputation: 31

Displaying a dialog on unhandled exception

I want to be able to display a dialog when my Xamarin Android application crashes. Something like 'Sorry application has crashed. Would you like to send an error report to the developer?".

I've hooked into Thread.DefaultUncaughtExceptionHandler which calls UncaughtException when an exception is caught.

When the exception is caught I try to start a new activity to display the dialog - the dialog is created but only a blank dialog with the title is shown. OnCreate is never called and the dialog seems to just hang.

If I call the code to display the dialog just normally when the application is running it displays and runs correctly so it doesn't seem to be anything wrong with the dialog code.

Anyone know how to solve this problem?

Upvotes: 3

Views: 5621

Answers (3)

Pradeep Kumar
Pradeep Kumar

Reputation: 1

I am able to display a Toast Message when App is getting crashed with the example you have provided. But I am not able to display alert window . I am writing the below code to display and alert message in the UnHandled Exception event for android devices.

 'alert.SetTitle("Un Handled Exception Event");
 SetMessage("Un Handled Exception Event triggered");
 SetCancelable(true);
 Show();'

Upvotes: 0

user3027550
user3027550

Reputation: 61

I found a way to catch unhandled exceptions. It depends on where the exception is thrown. If the exception occurs on a background thread it can be caugt by wiring up the following event in your class that inherits from the Mono Application class.

AppDomain.CurrentDomain.UnhandledException

I think that your app has crashed and there is no context available for the system to create your new activity and show your dialog.

I have used the example in the link below and as long as the exception happens off the main ui thread I am able to recover and show toasts so I feel confident it could be used to do what you need.

More information: http://xandroid4net.blogspot.com/2013/11/how-to-capture-unhandled-exceptions.html

Upvotes: 1

Michal Dobrodenka
Michal Dobrodenka

Reputation: 1134

Currently there is no supported and recommended way to catch and identify unhandled exception in Xamarin.Android. Hope they gonna fix this ASAP.

See http://mono-for-android.1047100.n5.nabble.com/Catch-all-the-exceptions-td5165993.html

See Jonathan Pryor-2 post.

Upvotes: 1

Related Questions