Reputation: 2090
When my app crashes for any reason onDestroy()
method is not called. My notification is also not removed. Where should notifi.cancel(1);
method be called to remove the notification whenever the app crashes?
@Override
protected void onDestroy() {
super.onDestroy();
if (nm != null) {
nm.cancel(0);
}
}
Upvotes: 1
Views: 2936
Reputation: 15414
Unfortunately onDestroy()
is not called when the app is crashed. To get a callback before the app is crashed, you should use an Exception Handler as mentioned here. You should write the code for removing the notification in an Exception Handler.
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
new Thread() {
@Override
public void run() {
Looper.prepare();
// Cancel Notification
if (nm != null) {
nm.cancel(0);
}
Looper.loop();
}
}.start();
}
});
Also you might want to take a look at this very similar question.
Upvotes: 4
Reputation: 839
onDestory()
method is called when system is low memory or when you call finish()
method. So when your app crashed, will not call onDestory()
method.
check this check this also
where is best position for run notifi.cancel(1); method?
You can call any where notifi.cancel(1);
. This all demand on what is needed.
Upvotes: 0