0

I have created multiple Activities and Fragments along with different threads in my application. Its hard to predict the application crash at sometime. Is there anything will help me to catch all the crashes globally at Main Activity or Manifest level ?

Tried putting try catches in all the Activities so that it catches any crash occurs in its child Fragment. But unfortunately its not happening.

Shawn
  • 19
  • 4

1 Answers1

1

You can register crash handler in app level I suggest use this code in your Application class

public class CrashHandler implements Thread.UncaughtExceptionHandler {

    public static final String TAG = CrashHandler.class.getSimpleName();
    private static CrashHandler instance = new CrashHandler();
    private Context mContext;

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        return instance;
    }

    public void init(Context context) {
        mContext = context;
        Thread.setDefaultUncaughtExceptionHandler(instance);
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        //crash info and exceptions here
    }
}

And in your Application class

public App  extends Application{

     @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler.getInstance().init(this);
    }
   
}

Last step, register your owner custom application class in your android manifest file

Enowneb
  • 943
  • 1
  • 2
  • 11
Godrick
  • 71
  • 5