return object of getContext

Context is an interface to global information about an application environment.

Activity class extends ContextThemeWrapper, if you want your activity as a parameter, you need to cast the context to your activity or create your Interface between your components.

If you want to know why, you need to read the source code:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/LayoutInflater.java

At line 758:

if (themeResId != 0) {
     context = new ContextThemeWrapper(context, themeResId);
}

And at line 908:

if (hasThemeOverride) {
        context = new ContextThemeWrapper(context, themeResId);
}

So… if you want to pass your activity as parameter, you shouldn’t use context.

Leave a Comment