I've wanted to avoid the app crash and then I develped the uncaughtException method to call another activity; it works fine. If the app crashes whatever activity, the uncaughtException is called and then my ErrorActivity is shown up. The problem is when I try to close the app after a crash. When I press the back button, the ErrorActivity is closed, but then a white screen with actionbar shows up and after a few seconds the ErrorActivity is called again. So, there is no way to close the app, except if finalize the app in the taskmanager.
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
}
public void handleUncaughtException (Thread thread, Throwable exception)
{
StringWriter _stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(_stackTrace));
Intent _intent = new Intent();
_intent.setAction ("com.mypackage.ERROR_ACTIVITY");
_intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
_intent.putExtra("error", _stackTrace.toString());
startActivity(_intent);
System.exit(0);
}
I found out the solution. It's just add a Intent.FLAG_ACTIVITY_CLEAR_TASK
like following:
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
}
public void handleUncaughtException (Thread thread, Throwable exception)
{
StringWriter _stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(_stackTrace));
Intent _intent = new Intent();
_intent.setAction("br.com.magazineluiza.mobilevendas.ERROR_ACTIVITY");
_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Here is the solution
_intent.putExtra("error", _stackTrace.toString());
startActivity(_intent);
System.exit(0); // or System.exit(1) It doesn't matter
}