Android App Restarts upon Crash/force close Android App Restarts upon Crash/force close android android

Android App Restarts upon Crash/force close


  1. Best solution would be instead of using Static data, use Shared Preferences or store data in a Database and if any uncaught Exception occurs, show a message like Application has crashed and a report is sent to the admin and then restart the Activity that caused the Crash. This way user can continue using the application.

  2. Do the same but instead of restarting the Activity which caused the Exception restart the application.

create a class used to handle unCaughtException

public class MyExceptionHandler implements        java.lang.Thread.UncaughtExceptionHandler {    private final Context myContext;    private final Class<?> myActivityClass;    public MyExceptionHandler(Context context, Class<?> c) {        myContext = context;        myActivityClass = c;    }    public void uncaughtException(Thread thread, Throwable exception) {        StringWriter stackTrace = new StringWriter();        exception.printStackTrace(new PrintWriter(stackTrace));        System.err.println(stackTrace);// You can use LogCat too        Intent intent = new Intent(myContext, myActivityClass);        String s = stackTrace.toString();        //you can use this String to know what caused the exception and in which Activity        intent.putExtra("uncaughtException",                "Exception is: " + stackTrace.toString());        intent.putExtra("stacktrace", s);        myContext.startActivity(intent);        //for restarting the Activity        Process.killProcess(Process.myPid());        System.exit(0);    }}

and in every Activity create an Object of this class and set it as the DefaultUncaughtExceptionHandler

    Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,            YourCurrentActivity.class));


public class MyApp extends Application {    private static final String TAG = "MyApp";    private static final String KEY_APP_CRASHED = "KEY_APP_CRASHED";    @Override    public void onCreate() {        super.onCreate();        final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();        Thread.setDefaultUncaughtExceptionHandler( new UncaughtExceptionHandler() {            @Override            public void uncaughtException(Thread thread, Throwable exception) {                // Save the fact we crashed out.                getSharedPreferences( TAG , Context.MODE_PRIVATE ).edit()                    .putBoolean( KEY_APP_CRASHED, true ).apply();                // Chain default exception handler.                if ( defaultHandler != null ) {                    defaultHandler.uncaughtException( thread, exception );                }            }        } );        boolean bRestartAfterCrash = getSharedPreferences( TAG , Context.MODE_PRIVATE )                .getBoolean( KEY_APP_CRASHED, false );        if ( bRestartAfterCrash ) {            // Clear crash flag.            getSharedPreferences( TAG , Context.MODE_PRIVATE ).edit()                .putBoolean( KEY_APP_CRASHED, false ).apply();            // Re-launch from root activity with cleared stack.            Intent intent = new Intent( this, MyRootActivity.class );            intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK );            startActivity( intent );        }    }}


  setContentView(R.layout.current);  Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {  @Override  public void uncaughtException(Thread t, Throwable e) {        android.os.Process.killProcess(android.os.Process.myPid());        System.exit(0);  }  code....

(reference: Archie.bpgc)