Posted on

ContextWrapper ContextImpl getBaseContext

ActivityThread casts Application.getBaseContext() to ContextImpl

static ContextImpl getImpl(Context context) {
        Context nextContext;
        while ((context instanceof ContextWrapper) &&
                (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
            context = nextContext;
        }
        return (ContextImpl)context;
    }

Potential crash when !(context instanceof Activity)

public static Activity unwrap(Context context) {
    while (!(context instanceof Activity)) {
        ContextWrapper wrapper = (ContextWrapper) context;
        context = wrapper.getBaseContext();
    }
    return context;
}
Leave a Reply

Your email address will not be published. Required fields are marked *