【发布时间】:2015-07-13 11:23:30
【问题描述】:
通过一些所谓的“好”资源来了解 Android 中上下文处理的细节和技巧,我多次遇到一种我无法理解的模式。
当您同样可以很好地使用隐式上下文时,使用 ContextWrapper 有什么优势?
例如为什么在活动方法中使用以下内容(直接在活动类中定义)
...
ContextWrapper cw = new ContextWrapper(getApplicationContext())
File filesDir = cw.getFilesDir();
...
不仅仅是
...
File filesDir = getFilesDir();
...
尽管 getFilesDir() 是在 ContextWrapper 类中定义的,但 Activity 无论如何都是 ContextWrapper 的子类,因此您仍然可以直接访问该方法。
那么这种增加的复杂性解决了哪些潜在问题(我没有看到)?
【问题讨论】:
-
Application也扩展了ContextWrapper。