I have built an Android application. If I terminate my application regularly trough the back button then onDestroy()
is executed. But when I kill my application (using the recent apps button), onDestroy()
is never executed. I have now added in the onCreate(Bundle savedInstanceState)
method of my main Activity
the following:
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
Later in the main Activity
class I have added:
private boolean isUIThread(){
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
public void handleUncaughtException(Thread thread, Throwable e) {
if(isUIThread()) {
// exception occurred from UI thread
} else { //handle non UI thread throw uncaught exception
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//handle non UI thread throw uncaught exception
}
});
}
}
Unfortunately, the handleUncaughtException()
method gets never called when I kill my application (it also gets not called when I regularly terminate my application through the back button).
What is wrong?
I'm using Android 5.1.