I need to determine, if my application was force closed, crashed, or terminated normally. My question is: How would you do such a thing in Android environment. Handling exceptions is easy, I only need to Replace Thread.setDefaultExceptionHandler. The hard part is: how can i distinguish a force close shut down from a "normal" (like back press, or home button + lots of time, etc)?
The reason is: We can synchronize the content of the local database, to a server. And we have a good reasons to allow this operation, only in state 1, of the Main activity, and not in state 2. However, if the user finds some db related semantic error in the program, in state 2 of the Main activity, then he or she can stuck there.
We're prepared for incoming call interruption, and other configuration changes, so, if the application gets stuck in state 2, then force closing the app, then reopening it, will still leave the user in stage 2, instead of stage 1.
So we want to leave a way, for the user, after a force closed stuck to go back into stage 1, and be able to synchronize the database, so the majority of his or her work can be saved.
Perhaps you could write a file on startup that you remove on shutdown. If the app starts and this file is present, you could conclude that it was force-closed.
Unfortunately there is no way. When a user or the system force stops the application, the entire process is simply killed. There is no callback made to inform you that this has happened. Even
onDestroy()
is not guaranteed to be called. That is documented in the documentation:"There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it ..."