code: public static AppCompatActivity getAppCompActivity(Context context) {
          if (context == null) return null;
          if (context instanceof AppCompatActivity) {
              return (AppCompatActivity) context;
          } else if (context instanceof ContextThemeWrapper) {
              return getAppCompActivity(((ContextThemeWrapper) context).getBaseContext());
          }
          return null;
      }
docs: Get AppCompatActivity from context
  
  @param context context
  @return AppCompatActivity if it's not null