I know this question is a bit outdated, but for future links and googlers, here is a complete answer from another, itβs hard to find, stackoverflow question: Android application restarts after failure / closing force
create a class used to handle unCaughtException
public class MyExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler { private final Context myContext; private final Class<?> myActivityClass; public MyExceptionHandler(Context context, Class<?> c) { myContext = context; myActivityClass = c; } public void uncaughtException(Thread thread, Throwable exception) { StringWriter stackTrace = new StringWriter(); exception.printStackTrace(new PrintWriter(stackTrace)); System.err.println(stackTrace);
Then in each thread (usually you have only one, unless you start a new thread (Async or ..., of course, you know about threads, if you can make a new one;)), set this class as DefaultUncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this, YourCurrentActivity.class));
Remember this! Do this at the last stage of your application development, you should at least try to process all your exceptions one by one before leaving them for DefaultUncaughtExceptionHandler
source share