Search code examples
androidexceptioncrashfirebase-crash-reportinguncaught-exception

How to prevent android app from being Crash


I want to stop my app from being crashed due to uncaught exception. I want exception to be store in log and prevent application from getting crashed. How do i achieve this ?. Thanks in advance.


Solution

  • For this your just need two classes Just copy paste them . This class is called when any exception occur within your application.

    public class CrashHandler implements
            java.lang.Thread.UncaughtExceptionHandler {
        private final Context myContext;
        private final String LINE_SEPARATOR = "\n";
    
        public CrashHandler(Context context) {
            myContext = context;
        }
    
        public void uncaughtException(Thread thread, Throwable exception) {
            StringWriter stackTrace = new StringWriter();
            exception.printStackTrace(new PrintWriter(stackTrace));
            StringBuilder errorReport = new StringBuilder();
            errorReport.append("************ CAUSE OF ERROR ************\n\n");
            errorReport.append(stackTrace.toString());
    
            errorReport.append("\n************ DEVICE INFORMATION ***********\n");
            errorReport.append("Brand: ");
            errorReport.append(Build.BRAND);
            errorReport.append(LINE_SEPARATOR);
            errorReport.append("Device: ");
            errorReport.append(Build.DEVICE);
            errorReport.append(LINE_SEPARATOR);
            errorReport.append("Model: ");
            errorReport.append(Build.MODEL);
            errorReport.append(LINE_SEPARATOR);
            errorReport.append("Id: ");
            errorReport.append(Build.ID);
            errorReport.append(LINE_SEPARATOR);
            errorReport.append("Product: ");
            errorReport.append(Build.PRODUCT);
            errorReport.append(LINE_SEPARATOR);
            errorReport.append("\n************ FIRMWARE ************\n");
            errorReport.append("SDK: ");
            errorReport.append(Build.VERSION.SDK);
            errorReport.append(LINE_SEPARATOR);
            errorReport.append("Release: ");
            errorReport.append(Build.VERSION.RELEASE);
            errorReport.append(LINE_SEPARATOR);
            errorReport.append("Incremental: ");
            errorReport.append(Build.VERSION.INCREMENTAL);
            errorReport.append(LINE_SEPARATOR);
    
            Intent intent = new Intent(myContext, ExceptionDisplay.class);
            intent.putExtra("error", errorReport.toString());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            myContext.startActivity(intent);
    
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(10);
        }
    
    }
    

    Now make an Activity that displayed the error :-

    public class ExceptionDisplay extends AppCompatActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.exception_display_layout);
    
            TextView exception_text = (TextView) findViewById(R.id.exception_text);
            Button btnBack = (Button) findViewById(R.id.btnBack);
            exception_text.setText(getIntent().getExtras().getString("error"));
    
            btnBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    intentData();
                }
            });
        }
    
        @Override
        public void onBackPressed() {
            intentData();
        }
    
        public void intentData() {
    
            Log.d("CDA", "onBackPressed Called");
            Intent setIntent = new Intent(ExceptionDisplay.this, AppDataSourceSelection.class);
            setIntent.addCategory(Intent.CATEGORY_HOME);
            setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(setIntent);
        }
    }
    

    Now you can call this class by the class that extends application Class:

    public class ErrorHandler extends Application {
    
    
        @Override
        public void onCreate() {
            super.onCreate();
           Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
    
            }}