1

I have following code to handle any uncaught exceptions and re-start the application from the Splash screen as there are a whole bunch of initilizations that I do in the Splash Screen. This is my launch screen.

Now I have the following code to this effect:

@Override
    public void uncaughtException(Thread thread, Throwable ex) {
        // restart Application
        Log.e("OSRAM Lightify", "LightifyApplication: UNCAUGHT EXCEPTION FOUND: \n" + ex.getStackTrace());

        Intent reStartIntent = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
        reStartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(reStartIntent);


    }

But is hangs at startActivity(reStartIntent); and screen goes blank.

Could some one please help me understand as to what is going on here?

ligi
  • 39,001
  • 44
  • 144
  • 244
Sunny
  • 7,444
  • 22
  • 63
  • 104

1 Answers1

0

there can be a lot causing this - after this you want to start fresh like so:

@Override
public void uncaughtException(final Thread thread, final Throwable ex) {

    LOG.error("", ex);

    installRestartIntent();

    System.exit(2);
}

private void installRestartIntent() {

    Intent rescueIntent = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    rescueIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    final PendingIntent pendingIntent = PendingIntent.getActivity(application,
                                                                  0,
                                                                  rescueIntent,
                                                                  rescueIntent.getFlags());
    final AlarmManager alarmManager = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, DateTime.now().plusSeconds(1).getMillis(), pendingIntent);
}

EDIT: be careful - some crashes might cause a loop - you might want to check this!

ligi
  • 39,001
  • 44
  • 144
  • 244