Search code examples
androidsqlitecrashsharedpreferencesterminate

How to clean data if Application crashes or is manual terminated?


When I was implementing last activity, a doubt came. My app stores and get some values from DB and SharedPreferences, so, is it possible to clean them if my App crashes or is terminated?

With terminated I mean like someone who goes in "Device Settings" and manually terminates App (or in other way). Maybe by a background check service?

EDIT - SOLVED by myself

Simply handling DefaultUncaughtExceptionHandler inside Application's onCreate:

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
  // here i start a new Activity in which clean and handle what i need)
}

Solution

  • I guess the best way would to check for "garbage" data when your app first load. your app will not be notified of anything else if it crashes or the user force closes it.