Accident Life Cycle Method - Android

I am developing an application that, in the event of a failure, should retain some data with the time of the failure. Now I save the data in onDestroy () as follows:

@Override protected void onDestroy() { saveState(); super.onDestroy(); } 

But whenever I break my application on purpose, onDestroy () is not called and my data is not saved.

My question is, how can I save my data on a crash? Which approach should I take? Because I need a breakdown time so that it is also saved, it is mandatory.

+5
source share
2 answers

UncaughtExceptionHandler is ideal for trap failures.

+4
source

Unfortunately, you cannot detect sudden crashes using the onDestroy or onStop . Your options:

  • Use try-catch blocks. Complete probable failure points in the try block and catch the exceptions in the catch block, then you can gracefully close the application.

  • Deploy try-catch blocks in vulnerable placements. You can then integrate crashlytics into your application and register exceptions in crashlytics in the catch block. Let the application be fully used by several users, and then in crashlytics analytics, you can see the places where the application actually threatens in most cases.

  • Use acra . It catches exceptions, extracts a lot of contextual data, and sends them to the backend of your choice.

You can always write the system time in the catch block and save your data there.

+2
source

All Articles