2

I have a class A which extends Application.In A i am handling uncaughtexceptions. Now the issue is whenever app encounters any issue app freezes and black screen appears before crashing

public class A extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    final AppContext context = AppContext.getInstance();
    context.setContext(this);
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable e) {
            mContext = context.getContext();
            e.getCause().getMessage();
            AppPreference.getInstance().setCrashReason(e.getMessage());
            Intent intent = new Intent ();
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            startActivity (intent);
            System.exit(1);
        }
    });
}

I have searched a lot but all went in vain.Thanks in advance.

ashlok malik
  • 65
  • 1
  • 9

1 Answers1

0

You can use the following way :

public class MyApplication extends Application
{
  public void onCreate ()
  {
    // Setup handler for uncaught exceptions.
    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
    {
      @Override
      public void uncaughtException (Thread thread, Throwable e)
      {
        handleUncaughtException (thread, e);
      }
    });
  }

public void handleUncaughtException (Thread thread, Throwable e)
  {
    e.printStackTrace(); // not all Android versions will print the stack trace automatically
       
        Intent intent = new Intent ();
        intent.setAction ("com.mydomain.SEND_LOG"); 
        intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
        startActivity (intent);
  }
}

according to this answer

Junior
  • 1,007
  • 4
  • 16
  • 26
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24