Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a custom view get access to its activity? [duplicate]

I have a custom view and would like to access a String that is available in its activity. I've seen code that uses getContext() in the view class, but there is no method to access the String that has been made available to its activity via an intent. How to make a String in an activity available to its custom view?

like image 567
turtleboy Avatar asked Oct 12 '11 20:10

turtleboy


1 Answers

The getContext() method in the View class returns the context that was passed on its constructor. Usually that's the Activity you want (Activity extends Context). So this probably works for you:

Java:

((Activity)getContext()).someMethod(...); 

Kotlin:

(context as? Activity)?.someMethod(...) 
like image 119
Tiago Simão Avatar answered Oct 11 '22 14:10

Tiago Simão