Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Add the ability to get the parent Activity of a View #593

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/androidx/core/view/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package androidx.core.view

import android.app.Activity
import android.content.ContextWrapper
import android.graphics.Bitmap
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -177,6 +179,22 @@ inline fun View.postOnAnimationDelayed(
return runnable
}

/**
* Returns the parent [Activity] of this [View].
*
* If this View is not attached to any [Activity], this method will return null.
*/
inline fun <reified T : Activity> View.getParentActivity(): T? {
var context = this.context
while (context is ContextWrapper) {
if (context is T) {
return context
}
context = context.baseContext
}
return null
}

/**
* Return a [Bitmap] representation of this [View].
*
Expand Down