如下:
public class MyApplication extends Application {
public static MyApplication context;
@Override
public void onCreate() {
super.onCreate();
context = this;
Thread.setDefaultUncaughtExceptionHandler(restartHandler);
}
private Thread.UncaughtExceptionHandler restartHandler=new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Process.killProcess(Process.myPid());
}
};
}