How to get Activity reference in View class?

CoDe picture CoDe · Oct 25, 2013 · Viewed 39.4k times · Source

I created an custom view and there require Activity reference to perform some Handler related operation. I have idea about getContext() is a way to get Context but is there any way to get Activity reference for same?

Answer

Jan picture Jan · Oct 25, 2013

It should be fine to just cast the context to Activity:

MyActivity myActivity = (MyActivity) getContext();