Handle application force close in android

Handle application force close in android

This is my sample coding is very helpful for you to handle the force close in Android applications.

Here, I have created a force close when clicking the button,
button1.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                     //Here App will restart instead of showing force close dialog
                     int i = Integer.valueOf("Android");
                     button1.setText(i);
               }

        });

And, then I have caught the exception using the

Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,  MainActivity.class));
Then, perform your action like restart the activity or anything.
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
Intent intent = new Intent(myContext, myActivityClass);
myContext.startActivity(intent);
System.exit(0);

Download

Leave a Reply

Your email address will not be published. Required fields are marked *