title: How to save stacktrace before application crash
body: <p>I need to store and send crash log to server. I am able to save crash log in preference before it exists, but the problem is that it restarts the application and does not display the default crash message to user. </p>
  
  <p>I want the crash flow for user to be normal. </p>
  
  <pre><code>public void registerCrash(){
  
      Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
      {
          @Override
          public void uncaughtException (Thread thread, Throwable e)
          {
              handleUncaughtException (thread, e);
          }
      });
  }
  
  
  public void handleUncaughtException (Thread thread, Throwable e)
  {
      thread.getStackTrace();
      saveDataToFile(e.toString());
      System.exit(-1);
  }
  </code></pre>
  
  <p>If i change the <code>System.exits(0)</code>, it abruptly crashes, and does not save any log in file, and with <code>System.exit(-1);</code>, it restarts the application on crash.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>saveDateToFile()</p>
  
  <pre><code>SharedPreferences sharedPreferences= context.getSharedPreferences(CRASH_LOG, Context.MODE_PRIVATE);
  SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
  prefsEditor.putString(STACK_TRACE, data);
  prefsEditor.apply();
  </code></pre>