Activity crash lifecycle method - android

試著忘記壹切 提交于 2020-07-06 09:48:10

问题


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.


回答1:


The UncaughtExceptionHandler is perfect for catching crashes.




回答2:


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.



来源:https://stackoverflow.com/questions/38349219/activity-crash-lifecycle-method-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!