Recently solved an Android app bug and found that the app was automatically restarted after being cleaned in the background. The phenomenon is very strange, some mobile phone (HTC) Backstage cleanup, the program must crash, and some cell phone (Xiaomi) program will not crash. When you look for a problem, you find that the HTC phone performs a background cleanup and the program restarts automatically, while the Xiaomi phone does not. Speculation may be the internal processing of millet phone, when the background cleanup, the corresponding app will not restart again. But the question is, why does the HTC phone background cleanup cause the program to crash.
Debug found that there is a service inside the app, in the service's Onstartcommand function, the return value is an int. The main two values are Start_sticky and start_not_sticky. The implication of Start_sticky is that if the service process is killed, the status of the reserved service is the start state, but the delivery intent object is not preserved. The system then tries to recreatethe service, and since the service status is started, the Onstartcommand (Intent,int,int) method must be called after the services are created. If no startup commands are passed to the service during this time, then the parameter intent will be null. And Start_not_sticky is "non-sticky". When this return value is used, the service is not automatically restarted if the service is killed by an exception after Onstartcommand is executed.
Our program is set to Start_sticky, the program timing restart, some variables are not initialized, resulting in crash. After changing to Start_not_sticky, the problem is solved. But there is still one problem, the boss found the app whether on the HTC or Xiaomi Machine, after performing a background cleanup, the service is still there, it is very strange, colleagues guessed that the phone ROM may be a specific program set up a white list. Personally, it's unlikely, but the question is, if all app coder can set the program to not be killed, why just not kill? Or our technology is not perfect, did not find the method of not being killed by the system?
In short, the Android system is very messy, or Apple Dafa good, we all behave according to the rules.
The program automatically restarts after the Android background cleanup app is resolved