Pregunta

I'm facing a similar issue like this one. I'm using some static class members inlcuding singletons in my aplication. When I leave the main activity using finish() and start it again, all static members are still present. It seems not to be a good soloution to call Process.killProcess(Process.myPid()); and I also don't want to need to unset all static members when I leave the application because this is error-prone when extending the application. So how would one completely quit an android application properly?

¿Fue útil?

Solución

Just some important observations on this topic (as I think):

This is an important aspect of Android programming. Checking the activity lifecycle documentations here http://developer.android.com/reference/android/app/Activity.html does not really do justice on explaining this behaviour well - as I think.

There is the underlying class-loader concept, which loads the class incl all your initial settings of static variables. Once the activity stops and even gets destroyed it may still remain in memory!

When you now start the app again, the last state of the class will be taken - if it still can be found in memory. It is NOT the case that the class will be initialized like the very first time when the class loader loads and initialized your class.

This is also the reason why during development/debugging restarting the app really "cleans" the complete class before starting while afterwards when the app is installed and started/quit on the device you may not force a "real" start like during debugging.

Nullifying variables just increases the probability for GC. You may not force an unload afaik. So there is no way to "completely quit an app" in that sense.

One has to design the app - esp the more complex ones - around this system behaviour.

This is my understanding so far - unfortunately I have not found any good thorough documentation on this.

If I should be wrong - please forgive and correct!

Otros consejos

You leave the static data members alone. They are not harming anyone. Android will terminate your process when it needs the memory.

As @CommonsWare told you can leave you static member and Android OS will take care of it and if you still need, you can overide onDestroy() and nullify all your static variables. THen it will be null when your app launches next time .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top