Callback before Force Close of Android Activity?

后端 未结 2 814
傲寒
傲寒 2021-01-14 04:58

I\'d like to have some emergency cleanup code execute just before my app crashes. I tried using onDestroy(),, onFinal() and finalize() to no avail. Is this possible in an An

相关标签:
2条回答
  • 2021-01-14 05:22

    just implement Error handling, and call your cleanup code.

    try {
    ...
    ...
    ...
    } catch (Exception e) { 
    cleanupcode();//cleanup code execute just before my app crashes
    e.printStackTrace();
    }
    

    you will not see the Force Close Dialog but if you want to force to close just call

       super.finish();
    
    0 讨论(0)
  • 2021-01-14 05:37

    Well, I would recommend not having your app crash in the first place. If there's something that COULD crash, just put a try/catch around it and handle it properly.

    Or, as some sort of global try/catch, you can use Thread.setUncaughtExceptionHandler(). Finally, you could even consider Runtime.addShutdownHook, but that's most likely a bad idea.

    0 讨论(0)
提交回复
热议问题