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 Android Activity?
            Asked
            
        
        
            Active
            
        
            Viewed 3,772 times
        
    3
            
            
        - 
                    1Force close = Activity Badly written. – Jorgesys Jul 22 '10 at 23:05
 - 
                    5@Jorgesys, either every single app I've ever used is "badly written" or there are unforeseen circumstances that cause all apps to crash (preferably not very often). – Yevgeny Simkin Nov 30 '11 at 21:17
 - 
                    1Badly Written, read this... http://developer.android.com/intl/fr/guide/practices/design/responsiveness.html I have recently released a new version of my app and all of the force close messages displayed after the latest version now are gone, the implementation of Asynchtask will do the job even with a slow connection http://developer.android.com/intl/fr/reference/android/os/AsyncTask.html – Jorgesys Nov 30 '11 at 22:31
 
2 Answers
2
            
            
        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.
        EboMike
        
- 76,846
 - 14
 - 164
 - 167
 
- 
                    I'll give this a try. I'm using a hardware accelerator so when Android loads my new program it doesn't call stop or anything and that causes my other process to be in the wrong state. – Frank Aug 02 '10 at 17:40
 
0
            
            
        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();
        Jorgesys
        
- 124,308
 - 23
 - 334
 - 268