Why would Application sometimes restart on killProcess?
Asked Answered
D

4

11

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)

Doyledoyley answered 30/7, 2012 at 16:57 Comment(1)
You have broadcast receivers with intent-filters registered in the Manifest? That would cause your app to start.Onshore
H
4

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

Horme answered 3/8, 2012 at 7:37 Comment(1)
The app, by the way, would sometimes restart even when ran first time from the installer or Eclipse. It doesn't restart always on those invocations.Doyledoyley
B
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.

Bluish answered 30/7, 2012 at 17:44 Comment(9)
Wow. Thanks for the great tips. I always thought I was running in a single process but let me double-check that. I will update as I have more findings.Doyledoyley
OK, I double-checked and I am running in a single process only. I do however invoke activities that run in other process (e.g. Google Maps), does that count? I am inclined to accept your answer because it contains links to cases very similar to mine with information that may explain what happens in my case. But I will wait a little to see if someone can come up with a more pinpointing explanation (I doubt this will happen). Thanks!Doyledoyley
Update: I just encountered yet another such restart incident and now that I have a few more log calls in place I can tell you that: 1. It always happens on a totally fresh install (i.e. app first uninstalled manually via Applications settings, then run via Run > Run (Ctrl+F11). 2. The restarts creates a new instance of the application (Activity.getApplication().toString()). It's definitely not the same application brought back to the front of the stack. What can I learn from this?Doyledoyley
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 code.google.com/p/android/issues/detail?id=2373 and code.google.com/p/android/issues/detail?id=26658Horme
@DavidWasser You are a genius. I wish you posted your comment as an answer so that I could accept it as the true answer to my ordeals. Unbelievable. I was sure all that time that this is caused by a fundamental problem in my code. Now I see that this is a 3 YO bug. How could Google let something like this happen?Doyledoyley
@DavidWasser you can post your comment as an answer so the OP can accept it... I won't take it personally :)Bluish
@Doyledoyley Glad to be of help! I posted my comment as an answer.Horme
@AlexLockwood I've taken up your offer. I guess I owe you one now ;-)Horme
Your answers both deserve to be accepted: Without Alex's answer I wouldn't have been able to reach the point in which David made his discovery. But for the sake of other SO members, I will re-accept David's as it can help the innocent passerby more quickly grasp the answer for this particular and very specific situation.Doyledoyley
T
1

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

Tryst answered 30/7, 2012 at 18:9 Comment(0)
D
0

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

Death answered 24/4, 2014 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.