android - Why use ContextWrapper directly in an Activity instead of implicit context from "this" -
going through supposedly "good" sources sources learn details , tricks of context handling in android have come across 1 pattern multiple time fail understand.
what advantage of using contextwrapper when equally use implicit context?
for example why use following in activity method (defined directly in activity class)
... contextwrapper cw = new contextwrapper(getapplicationcontext()) file filesdir = cw.getfilesdir(); ... instead of just
... file filesdir = getfilesdir(); ... even though getfilesdir() defined in contextwrapper class activity anyway subclass of contextwrapper have direct access method anyway.
so potential issue (that fail see) added complexity address?
i'd (and might wrong) in scenario (and context) presented might not make difference. getapplicationcontext().getfilesdir() have been used easily.
however, believe contextwrapper might useful in other scenarios. understand, adapter pattern. may want provide different behaviour methods while proxying other original context reference pass in.
check out piece of code remoteviews:
// remoteviews may built application installed in // user. build context loads resources user // still returns current users userid settings data / time formats // loaded without requiring cross user persmissions. final context contextforresources = getcontextforresources(context); context inflationcontext = new contextwrapper(context) { @override public resources getresources() { return contextforresources.getresources(); } @override public resources.theme gettheme() { return contextforresources.gettheme(); } };
Comments
Post a Comment