Why would Application sometimes restart on killProcess?

会有一股神秘感。 提交于 2019-12-06 21:52:09

问题


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)


回答1:


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




回答2:


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.




回答3:


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()




回答4:


Please follow the link it has the expected answer of your question. android.os.Process.killProcess(pid) did restart the processes again



来源:https://stackoverflow.com/questions/11726100/why-would-application-sometimes-restart-on-killprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!