Posts Tagged ‘Activity’

Access Activity class from View

Click here to read Access Activity class from View

So, it begins! This is the first on this blog!

Let’s start with a Android Programming tip: how to access the Activity from an instantiated View object.

The first thing to do is consider if you really need to call an Activity from a View. Take some time to analyze your classes and their relationships. Probably, the variable/method you need to access doesn’t have to be a member of the Activity class. In that case, think about placing that variable/method inside the View or create a new class that has variables/methods which can be accessed by both Activity and View. Unless an Activity variable or method (e.g. Activity.finish() ) needs to be called from the View, there is no need to try to access the Activity from the current View.

But if you need to access a method/variable of the calling Activity from the View, here is how it’s done: