Ordinarily, exiting my application by calling:
android.os.Process.killProcess(android.os.Process.myPid());
performs well without incident.
But every once in a while, the application will restart again (after exiting!).
The relevant log snippet shows:
.631: I/Process(15495): Sending signal. PID: 15495 SIG: 9
.641: W/AudioFlinger(121): write blocked for 252 msecs, 1279 delayed writes, thread 0xdc18
.651: I/ActivityManager(164): Process com.ef.myapp (pid 15495) has died.
.651: I/WindowManager(164): WIN DEATH: Window{463659e8 com.ef.myapp/com.ef.myapp.MainActivity paused=false}
.661: I/AudioService(164): AudioFocus abandonAudioFocus() from android.media.AudioManager@460b2b98
.701: I/ActivityManager(164): Start proc com.ef.myapp for activity com.ef.myapp/.MainActivity: pid=15589 uid=10077 gids={3003}
I know that, by the design of the Android OS, killProcess() isn't the proper way to terminate an application. This is because killProcess() stops the process immediately without giving any way or chance for the app to prevent it or prepare for it.
I know that when I call finish(), the application stack is just pushed to the background (and still exists in the memory). Android itself decides when to close the application (i.e. remove its instance from the memory) and generally this is done when the application becomes "the oldest not used for the longest time". Its behavior is actually more predictable if it's really the last one.
The problem is that finish() only stops and destroys the activity for which it was called. It doesn't stop other activities spawned by the application or other activities. So, for ease of test & debug during development, I am using killProcess() as a convenient shortcut.
But now I see that this has the side effect of the application sometimes restarting immediately after killing itself -- all within 30 milliseconds.
A straightforward solution would be to iterate through all application's activities and finish() them. But before proceeding with this, I am dying to understand what in the Android OS makes an application resurrect itself.
Why would Android make a killed Application restart?
And why inconsistently? (i.e. sometimes)
There is a known bug in the way applications get started the first time from the installer, web-browser and via IDE (IntelliJ, Eclipse, etc.). Please try to install your app without starting it and then start it from the list of available applications and see if the problem goes away. See these issues filed long ago related to the problem: