3

I've implemented an UncaughtExceptionHandler in my Android code. If an error occurs, it first logs some diagnostic data, then it attempts to exit. I've followed the recipe in How to kill an application with all its activities? so that Activities cascade in closing.

This works when the error is in a non-UI thread, or when the main activity is active. However, if the error occurred on the UI thread on a subordinate Activity, the calling Activity never wakes up. onActivityResult() never gets called, and the application hangs. Is there any way to exit cleanly after the UI thread crashes?

Here's a simplified version:

public void uncaughtException( final Thread t, final Throwable e )
{
    Activity ac = currentActivity();
    ac.setResult( 99 );
    ac.finish();
}

in the main activity:

public boolean onOptionsItemSelected(MenuItem item)
{
    Intent intent = new Intent( getApplicationContext(), SettingsActivity.class );
    startActivityForResult( intent, 0 );
}

protected void onActivityResult( int rqC, int resultCode, Intent data )
{
    if ( resultCode == 99 )
    {
        setResult( 99 );
        finish();
    }
    super.onActivityResult( rqC, resultCode, data );
}
Community
  • 1
  • 1
Jerry Agin
  • 629
  • 1
  • 5
  • 15
  • Could you post some more of your code so that we can see what you are doing? – Mr. Concolato Dec 14 '14 at 23:14
  • Have you tried using finish() when you catch the exception? – AggieDev Dec 14 '14 at 23:59
  • Simplified code added via edit to post. – Jerry Agin Dec 15 '14 at 00:19
  • as the activity have crashed, i am not sure if you still can trigger that finish-sequence, if i where you, i would add a broadcast receiver in each activity, and then send a finish command from `uncaughtException()` so all activities will call `finish()` inside thier `onReceive()` , maybe without `setResult(99)` as all activities will close and no need for `onActivityResult()` to be triggered, I AM not sure if this is correct to do, or even if it will work, i will just give it a try – Yazan Dec 15 '14 at 07:45

0 Answers0