I want to quit my app if there is an uncaught exception. For that I'm using System.exit(10)
and Process.killProcess
(I know this is not a good practice to call these methods). My code is the following:
public class UncaughtExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {
String Tag;
Context myContext;
public UncaughtExceptionHandler(Context context, String TAG) {
Tag = TAG;
myContext = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
Logger.appendErrorLog(sw.toString(), Tag);
System.exit(10);
Process.killProcess(Process.myPid());
}
}
But the problem with this code is, after calling the exit method, my application again restarts(Home activity of app starts). I don't want my app to start again once exception comes. To resolve this issue I tried one more way:
public class UncaughtExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {
String Tag;
Context myContext;
public UncaughtExceptionHandler(Context context, String TAG) {
Tag = TAG;
myContext = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
Logger.appendErrorLog(sw.toString(), Tag);
Intent intent = new Intent(myContext, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("EXIT", true);
myContext.startActivity(intent);
}
}
After this I'm calling finish in my HomeActivity
. After running this code my application is not at all quitting and I'm getting a black screen
How can I kill the application either by using exit method or by finishing all activities?