Posted on Leave a comment

ContextWrapper ContextImpl getBaseContext

ActivityThread casts Application.getBaseContext() to ContextImpl

[code language=”java”]
static ContextImpl getImpl(Context context) {
Context nextContext;
while ((context instanceof ContextWrapper) &&
(nextContext=((ContextWrapper)context).getBaseContext()) != null) {
context = nextContext;
}
return (ContextImpl)context;
}
[/code]

Potential crash when !(context instanceof Activity)

[code language=”java”]
public static Activity unwrap(Context context) {
while (!(context instanceof Activity)) {
ContextWrapper wrapper = (ContextWrapper) context;
context = wrapper.getBaseContext();
}
return context;
}
[/code]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.