0

I want to restart my application after crash. So I created a crash handler on Application class:

    public final class MyApp extends Application {
    private static Thread.UncaughtExceptionHandler mDefaultUEH;


    private Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {


            Intent splashIntent = new Intent(getInstance().getActivity(), A1SplashScreen.class);

            //splashIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            //splashIntent.setAction(Intent.ACTION_MAIN);
            splashIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            splashIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            splashIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            int requestID = (int) System.currentTimeMillis();
            PendingIntent pending = PendingIntent.getActivity(getInstance().getActivity(), requestID, splashIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager) getInstance().getActivity().getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC, System.currentTimeMillis(), pending);

            mDefaultUEH.uncaughtException(thread, ex);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();

        Fabric.with(this, new Crashlytics());
        mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
    }
}

A1SplashScreen is main activity of the application, so I want to start that activity after crash. There is no problem on API 21 and above. But problem is on API level lower than 21. After application crashes, A1SplashScreen launches but onCreate() method of it does not called. So the screen freezes and nothing shown (only white screen). It does not response and does not crash. Here is screenshot: enter image description here

Kinjal
  • 1,195
  • 4
  • 13
  • 24
cimenmus
  • 1,386
  • 4
  • 18
  • 31
  • This really isn't reliable. You could try adding some delay to allow the old process to properly shutdown before you restart your app. Try to have the alarm trigger after 5 or 10 seconds and see if that helps. – David Wasser Nov 29 '16 at 14:26

1 Answers1

0

RESOLVED

When I call System.exit(2) method, application restarted. But I could not see crash on crashlytics. On the other hand, if I do not call System.exit(), I can see crash on crashlytic but application does not restart. That's why I ran System.exit(2) method and mDefaultUEH.uncaughtException(thread, throwable) parallel via ExecutorService. Here is the working code :

 private Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

        Intent intent = new Intent(getApplicationContext(), A1SplashScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        getInstance().getActivity().startActivity(intent);

        activity.finish();

        ExecutorService executorService = Executors.newCachedThreadPool();
        CallCrashlytics callCrashlytics = new CallCrashlytics(thread, ex);
        CallSystemExit callSystemExit = new CallSystemExit();

        try {
            executorService.invokeAll(Arrays.asList(callCrashlytics, callSystemExit));
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
};

class CallCrashlytics implements Callable<Void>{

    Thread thread;
    Throwable throwable;

    CallCrashlytics(Thread thread, Throwable throwable){
        this.thread = thread;
        this.throwable = throwable;
    }

    @Override
    public Void call() throws Exception {
        mDefaultUEH.uncaughtException(thread, throwable);
        return null;
    }
}

class CallSystemExit implements Callable<Void>{

    @Override
    public Void call() throws Exception {
        System.exit(2);
        return null;
    }
}
cimenmus
  • 1,386
  • 4
  • 18
  • 31