简体   繁体   English

活动崩溃生命周期方法 - android

[英]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: 现在,我将这些数据保存在onDestroy()中,如下所示:

@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. 但是每当我故意崩溃我的应用程序时,都不会调用onDestroy()并且不会保存我的数据。

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. 不幸的是,您无法通过onDestroyonStop方法检测到突然崩溃。 Your options are: 你的选择是:

  1. Use try-catch blocks. 使用try-catch块。 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. 在try块中包含可能的崩溃位置并捕获catch块中的异常,然后您可以优雅地关闭应用程序。

  2. Implement try-catch blocks at susceptible placecs. 在易受影响的placecs上实现try-catch块。 Then, you can integrate crashlytics to your app and log the exceptions to crashlytics in the catch block. 然后,您可以将crashlytics集成到您的应用程序中,并将异常记录到catch块中的crashlytics中。 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 . 使用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. 您始终可以在catch块中记录系统时间并将数据保存在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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