在视图中声明内部类危险吗?
我读过Android官方技术文章:“如何避免内存泄漏?”
。他们说在 Activity 类中创建内部类很危险,但是在 View 类中创建内部类又如何:泄漏危险是相同还是更严重?
I've read the official Android technical article: "how to avoid memory leaks ?"
. They say that creating inner classes in an Activity
class is dangerous, but what about creating inner class inside a View
class : is the leak danger the same or worse ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

如果您创建的类的生命周期不超过视图,那就没问题。否则你应该避免这样做。
如果您使用这种结构 - 当不需要视图时,请确保您没有内部类实例上的任何链接。然后GC会收集所有东西,并且不会出现内存泄漏。
If you create classes with lifecycle not more than view - it's OK. Otherwise you should avoid doing this.
If you use such structure - be sure, that you do not have any links on instances of inner class, when view is not necessary. Then GC will collect everything, and there will not memory leaks.