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

Commit b371ca8

Browse files
author
gahfy
committed
Add the ability to get the parent Activity of a View
1 parent 5100588 commit b371ca8

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

  • src/main/java/androidx/core/view

src/main/java/androidx/core/view/View.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package androidx.core.view
2020

21+
import android.app.Activity
22+
import android.content.ContextWrapper
2123
import android.graphics.Bitmap
2224
import android.view.View
2325
import android.view.ViewGroup
@@ -201,6 +203,22 @@ fun View.toBitmap(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap {
201203
}
202204
}
203205

206+
/**
207+
* Returns the parent [Activity] of this [View].
208+
*
209+
* If this View is not attached to any [Activity], this method will return null.
210+
*/
211+
inline fun <reified T:Activity> View.getParentActivity() : T? {
212+
var context = this.context
213+
while (context is ContextWrapper) {
214+
if (context is T) {
215+
return context
216+
}
217+
context = context.baseContext
218+
}
219+
return null
220+
}
221+
204222
/**
205223
* Returns true when this view's visibility is [View.VISIBLE], false otherwise.
206224
*

0 commit comments

Comments
 (0)