How to stop my application from zombie after I process unexposed Excepition?

I handle any uncaught exceptions that occur in my application through:

Thread.setDefaultUncaughtExceptionHandler(this); 

were these my launch actions implementing the UncaughtExceptionListener. I handle the exception and send it to my journal, just fine, however my application does not end there. It just runs like a zombie until the home or back button is pressed. How can I kill a process after handling an exception?

Edit
Here is a test and work activity that throws an uncaught exception that needs to be caught (by Thread) and handled. The toast is never published, and the process becomes a zombie after registering an exception.

 class TestActivity extends Activity implements UncaughtExceptionListener { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Thread.setDefaultUncaughtExceptionHandler(this); throw new RuntimeException("I'm a destuctive booger."); setContentView(R.layout.activity_test); } @Override public void uncaughtException(Thread thread, Throwable toss) { Log.e(TAG, "An uncaught exception has been recieved.", toss); Toast.makeText(this, "Big Error", Toast.LENGTH_LONG).show(); } } 
+6
source share
2 answers

I did some testing with your sample code and found that finish() would work with uncaughtException() . However, it was silent, so it did not give feedback to the user, and, like you, I could not call Toast or any other visual message window even with getApplicationContext() .

The only way to send any feedback I could get is to use the NotificationManager to send the Notification status bar using getApplicationContext() for Context .

I did not spend much time on this, but, unfortunately, this means that you need to provide a PendingIntent for processing when the user clicks on the notification itself. Doing this with the same Activity can obviously lead to an infinite loop if the Activity for the PendingIntent is the one that throws the unhandled exception.

However, it would be possible to create a basic Activity option with a dialogical theme that would be open and explain the failure.

+1
source

Not sure if there is a milder way to solve this problem, but you can try:

 import android.os.Process; Process.killProcess(Process.myPid()); 
0
source

Source: https://habr.com/ru/post/912526/


All Articles