This would be happen via SharedPreferences
, first of all when you just enter your app in the MainActivity
create a boolean
variable called crash
and save it to your SharedPreferences
with a value of false
, then when catching a crash, just resave this variable with the value true
and this will automatically override the crash
value stored before.
To save the value:
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean("crash", false);
editor.commit();
}
To load the saved value:
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
boolean crash = sharedPreferences.getBoolean("crash", false);
if(crash){
// then your app crashed the last time
}else{
// then your app worked perfectly the last time
}
}
So, in your crash handler class, just save the value to true:
p.s. this must run for all unHandled Exceptions whatever from the app of from the OS.
public class CrashHandler extends Application{
public static Context context;
public void onCreate(){
super.onCreate();
CrashHandler.context = getApplicationContext();
// Setup handler for uncaught exceptions.
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
}
public void handleUncaughtException (Thread thread, Throwable e)
{
e.printStackTrace(); // not all Android versions will print the stack trace automatically
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
Editor editor = sharedPreferences.edit();
editor.putBoolean("crash", true);
editor.commit();
}
}