Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - callback function for handling crashes

Tags:

android

I need to handle crashes in android application,Is there any callback function or overridden method in activity class which will be called when the app crashes? Guide me in solving this...

like image 206
Divya Priya Avatar asked Jun 01 '26 07:06

Divya Priya


1 Answers

You can set an uncaught exception handler which will be called every time. Like this

     final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            // do your magic
            defaultHandler.uncaughtException(thread, throwable);
        }
    });

You should probably subclass the Application class and run this code as the first thing in the OnCreate method. Getting the default handler and passing the exception along is to ensure proper handling after you are done performing magic.

What are you trying to achieve?

like image 131
Kenneth Avatar answered Jun 02 '26 23:06

Kenneth