Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would Application sometimes restart on killProcess?

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)

like image 941
ef2011 Avatar asked Jul 30 '12 16:07

ef2011


3 Answers

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:

http://code.google.com/p/android/issues/detail?id=2373

http://code.google.com/p/android/issues/detail?id=26658

like image 61
David Wasser Avatar answered Oct 23 '22 09:10

David Wasser


Is your app running in a single process, or multiple? killProcess will kill a single process, not necessarily your entire application. Try using ActivityManager#killBackgroundProcesses(String packageName) instead.

If that doesn't work, it looks like these links might be helpful in explaining the system's behavior when the process is killed.

And by the way, the Android system is what is restarting your application... it's fine to manipulate its behavior (i.e. by preventing apps from restarting on force close) for development purposes, but you shouldn't do this when you push your app to production.

like image 37
Alex Lockwood Avatar answered Oct 23 '22 10:10

Alex Lockwood


From ADT 17.0.0, there is a static field BuildConfig.DEBUG which will help you on debugging. For example you can have a static class which holds all instances of running activities. Then you can finish them all at a time. I think it's better than killProcess()

like image 1
SheIs_LeThiCongNhan Avatar answered Oct 23 '22 09:10

SheIs_LeThiCongNhan