getActivity

fun Context.getActivity(): Activity?

Recursively unwraps this Context to find the underlying Activity.

In Android, a Context is frequently wrapped in multiple layers of ContextWrappers (e.g., for Themes, Hilt/Dagger, or Jetpack Compose). Attempting to cast a wrapped context directly to Activity can cause a ClassCastException.

This function traverses the baseContext chain until it finds the actual Activity or reaches the end of the chain.

Return

The underlying Activity if found, or null if the context is not attached to an activity.