且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

静态类变量的生存期

更新时间:2022-06-27 00:19:53

根据您所描述的症状,什么是最有可能发生的是,检查的类加载器越来越垃圾收集,但你的活动不被垃圾收集。因此,当从背景应用程序返回时,活动进行到 onResume 。当调用 Checker.check()制成,在检查类重新装入值1 和值2

Based on the symptoms you are describing, what's most likely happening is that the ClassLoader for Checker is getting garbage collected but your Activity is not being garbage collected. So when the app returns from the background, Activity goes to onResume. When the call to Checker.check() is made, the Checker class is reloaded with the default values of value1 and value2.

一个办法,以确保检查类,只要是活动保持活动类将检查实例的引用保持周围。当然,您也可以考虑重新设计检查功能,因此,它不依赖于静态成员的行为。

One way to make sure that the Checker class stays around as long as the Activity is to keep a reference to a Checker instance in the Activity class. Of course, you might also consider redesigning the Checker functionality so that it doesn't depend on static member behavior.