简体   繁体   中英

Activity crash lifecycle method - android

I'm developing an app which in a case of crash, needs to save some data with the time of the crash. Now, I'm saving the data in onDestroy() like this:

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

But whenever I crash my app 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 the time of the crash to be also saved, it's mandatory.

UncaughtExceptionHandler非常适合捕捉崩溃。

Unfortunately, you can't detect sudden crashes through onDestroy or onStop methods. Your options are:

  1. Use try-catch blocks. Wrap up the probable places of crash in try block and catch the exceptions in the catch block, then you can gracefully close the app.

  2. Implement try-catch blocks at susceptible placecs. Then, you can integrate crashlytics to your app and log the exceptions to crashlytics in the catch block. Let the app used thoroughly by a couple of users and then at crashlytics analytics, you can see the places where the app is actually crashing most of the times.

  3. Use acra . It catches exceptions, retrieves lots of context data and send them to the backend of your choice.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM